Re: Trigger function failure
От | Fábio Moreira |
---|---|
Тема | Re: Trigger function failure |
Дата | |
Msg-id | CANQddpN5NR-bVzbPQ38mePh1YQeWQCLjeqzZ6FXj=qZn5LtHWQ@mail.gmail.com обсуждение исходный текст |
Ответ на | Trigger function failure (Michael Rowan <michael.rowan3@gmail.com>) |
Список | pgsql-novice |
Hi Michael,
From http://www.postgresql.org/docs/current/static/functions-aggregate.html:It should be noted that except for
count
, these functions return a null value when no rows are selected. In particular, sum
of no rows returns null, not zero as one might expect, and array_agg
returns null rather than an empty array when there are no input rows.This explains why your query works with COUNT(): that function *is* guaranteed to return 0 when run over an empty set.
COALESCE((SELECT …), 0)
(SELECT …)
[]s, Fábio.
On Mon, Mar 7, 2016 at 3:43 PM, Michael Rowan <michael.rowan3@gmail.com> wrote:
I have a trigger function problem that threatens to cause my early demise. Please someone help an absolute novice.I have two tables, invoice and invline, with the usual functions.Table invoice has a column for the total of debit lines, and a column for the total of credit (payment) lines. The function is triggered by any change in inv.grossThe ERROR occurs if there are no invline found by either summing part, where we set dr_total or cr_total.If I change "sum" to "count" it works as expected, returning 0.00--THE FUNCTIONUPDATE invoice SETinvoice.cr_total = (SELECTsum(invline.gross)FROM invlineWHERE invline.type > 4 AND invline.invoice_id = 200003),invoice.dr_total = (SELECTsum(invline.gross)FROM invlineWHERE invline.type <5 AND invline.invoice_id = 200003)WHERE invoice.id = 200003;--TESTselect invoice.dr_total, invoice.cr_total from invoice where invoice.id=200003---------------------------------------------------------------------------------------------ERROR: null value in column "po_cr_total" violates not-null constraint
Anyone?
Mike
--
Fábio Dias Moreira
В списке pgsql-novice по дате отправления: