3

Below trigger executes with out any error, but throws

ERROR:  relation "new" does not exist

on inserting single row into tbl1

CREATE OR REPLACE FUNCTION update_tbl1() 
    RETURNS TRIGGER AS
    $BODY$ DECLARE
DECLARE no_of_rows INT;
BEGIN
    SELECT COUNT(*) as no_of_rows FROM NEW; --error location
         ...
         ...
    COMMIT TRANSACTION
;
END; 
 $BODY$ LANGUAGE plpgsql;
CREATE TRIGGER update_tbl2 AFTER INSERT ON tbl1
     EXECUTE PROCEDURE update_tbl1();

I am unable refer INSERTED, DELETED (SQL SERVER) rows in postgres using NEW and OLD, I appreciate any help.

I need to update a table similar to this

Table-Bill
item  cost  subtotal
itm1  1     1
itm2  5     6
itm3  4     10
itm4  3     13

My input is 'item' and 'cost' column, and trigger needs to update 'subtotal' with sum of all items inserted so far, I am not sure whether to use 'FOR EACH ROW' or for statement and then use cursor please help with solution with good performance

13
  • 2
    Unlike SQL Server, Postgres supports row triggers, there is no "inserted" table in a row trigger. What exactly are you trying to do? And another thing: you can not commit or rollback in a trigger function (or any function that is). Commented Mar 20, 2014 at 8:11
  • @safetyOtter "SQL" does not mean "Microsoft SQL Server". It is the query language. Don't assume that sql means sql-server. Commented Mar 20, 2014 at 8:12
  • @safetyOtter: that is not a valid identifier in Postgres (or any ANSI standard compliant DBMS) Commented Mar 20, 2014 at 8:12
  • oops, wasn't reading carefully, ty for the correction Commented Mar 20, 2014 at 8:16
  • 1
    What do you want to do with the result of the select count(*) ? maybe "assign" it somehow ? Commented Mar 20, 2014 at 11:56

1 Answer 1

3

FOR EACH STATEMENT does not support NEW and OLD usage so only way in current postgres is to go by FOR EACH ROW to access updated/inserted/deleted rows check this post for more information How to use 'for statement' triggers in postgres?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.