I have 2 tables in Firebird 3 db - master table "Docs" for output documents and detail table "Recs" for sold goods.
- Columns of
Docs
: doc_id, summa - Columns of
Recs
: id, doc_id, goods, price, qnt
When customer cancels sale this record remains in detail table, didn't remove it and after update trigger updates total summa
of master table.
Trigger works well before last record remains in output table. But if customer cancels last sale and detail dataset becomes empty trigger didn't work correctly and shows summa
of last record instead of 0.
How correct this trigger?
CREATE OR ALTER TRIGGER RECS_AU FOR Recs
ACTIVE AFTER UPDATE POSITION 0
AS
DECLARE VARIABLE Price DECIMAL(12, 4);
DECLARE VARIABLE Doc_id INTEGER;
DECLARE VARIABLE OLD_Price DECIMAL(12, 4);
BEGIN
select
r.Doc_id,
coalesce(sum(r.Price*r.Qnt),0)
from Recs r, Docs d
where r.Doc_id=new.Doc_id and r.Doc_id=d.Doc_id and r.status<>'deleted'
group by r.Doc_id
into :Doc_id, :Summa;
update Docs set
Docs.Summa=:Summa:
where Doc_id=:Doc_id;
I know that problem is in writing correct SQL, not in trigger. After dataset becomes empty it returns empty record set. Why doesn't work coalesce in case of empty dataset and doesn't show summa
as 0?
SELECT
doc_id,
coalesce(SUM(cast((r.price*r.qnt) as decimal(18,4))),0) as summa
FROM
recs r
WHERE
doc_id= :doc_id AND status <> 'deleted'
GROUP BY
doc_id
Recs
remaining, then the select statement in the Trigger returns no records, not 1 record with a zero value. You need to stop using equijoins. Use left-joins intead.