Search code examples
postgresqltriggerspsql

Why the Trigger is not working in Postgresql


I want to update the "noofdays" column with the below trigger. But it is not working and I've attached the trigger code and the table structure below

CREATE OR REPLACE FUNCTION update_days() RETURNS TRIGGER AS $$
  BEGIN
        new."noofdays":=1;
    RETURN new;
  END; 
$$ LANGUAGE plpgsql;
CREATE TRIGGER calc_days before UPDATE ON bookentry FOR EACH STATEMENT EXECUTE PROCEDURE update_days();
 bookid     | integer                     |           |          |
 loginid    | integer                     |           |          |
 borrowtime | timestamp without time zone |           | not null | now()
 returntime | timestamp without time zone |           |          |
 noofdays   | integer                     |           |          |

Solution

  • You need a row level trigger:

    CREATE TRIGGER calc_days 
       before UPDATE ON bookentry 
       FOR EACH ROW  --<< this
       EXECUTE PROCEDURE update_days();