1

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
3
  • If there are no applicable 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. Nov 20 at 8:53
  • 1
    Actually the problem is in doing SELECT at all instead of using NEW and OLD contexts. The trigger's logic is wrong. It must adjust "Summa" value using data from the current record only instead of calculating of the sum of all records. Nov 20 at 12:58
  • @FreddieBell If I exclude 'group by' it doesn't matter which join I will use- inner join or left join, both works correctly.
    – basti
    Nov 20 at 17:55

1 Answer 1

3

As Freddie Bell mentions in the comments, the problem is that you're using an implicit inner join. Meaning that when there are no rows in RECS for a row in DOCS, then there will be no row due to the presence of the GROUP BY.

You either need to use an explicit LEFT JOIN as suggested by Freddie, only select from RECS without joining DOCS (and leave out the GROUP BY), or even simpler, just perform an update with a correlated sub-select without first executing a select separately:

update DOCS d set d.Summa = (
  select coalesce(sum(r.Price*r.Qnt), 0)
  from RECS r
  where r.DOC_ID = d.DOC_ID and r.STATUS <> 'deleted'
)
where d.DOC_ID = :NEW.DOC_ID

This will work, even if there are no rows in RECS, because absent a GROUP BY, this will produce a row when there are no matching rows in RECS.

11
  • Such update will encounter visibility problem on concurrency. In most cases it should end up in update conflict error, but on forums I saw that some developers managed to get wrong sums silently. (I have no idea how, stupidity is too inclusive.) Nov 20 at 13:02
  • @user13964273 There will only be concurrency issues if multiple transactions modify the DOCS or RECS for the same DOC_ID concurrently, and if that is the case, they already have that problem with the existing trigger. Nov 20 at 14:19
  • @MarkRotteveel If I exclude 'group by' it doesn't matter which join I will use- inner join or left join, both works correctly.
    – basti
    Nov 20 at 17:58
  • @MarkRotteveel Excuse me but I was wrong- there are 4 sums in my SQL, not 1. I oversimplified my SQL. How my I use sub-select in this case?
    – basti
    Nov 20 at 18:03
  • 1
    @basti Yes, without a GROUP BY it will indeed work, because then the aggregate function will always produce a row, however, from a performance perspective, ask yourself: why would you execute two statements when one will do. Nov 21 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.