Home > database >  Why the Trigger is not working in Postgresql
Why the Trigger is not working in Postgresql

Time:05-08

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                     |           |          |

CodePudding user response:

You need a row level trigger:

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

CodePudding user response:

You should trig a row level trigger. Coming code can be helpful: CREATE TRIGGER calc_days before UPDATE ON bookentry FOR EACH ROW --<< this EXECUTE PROCEDURE update_days();

  • Related