Trigger for update timestamp field - sql

I'm trying to create trigger, that updates my lastmodified field on insert/update. Here's my trigger:
CREATE OR REPLACE TRIGGER timestamp_trigger BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE PROCEDURE timestamp_update;
and my procedure:
CREATE OR REPLACE FUNCTION timestamp_update()
RETURNS TRIGGER
AS
$$
BEGIN
UPDATE orders
SET lastmodified = getdate()
WHERE orderid = new.orderid
RETURN new;
END;
$$ language plpgsql;
Unfortunately when I try to update some field, field isn't updated. Also when i create row, the lastmodified field isn't updated.

Do not use update in a trigger like this because it may lead to endless recursive loop.
If you want to modify inserted/updated row, just change the new record:
CREATE OR REPLACE FUNCTION timestamp_update()
RETURNS TRIGGER
AS
$$
BEGIN
NEW.lastmodified = now();
RETURN new;
END;
$$ language plpgsql;
The syntax create or replace trigger is incorrect.
CREATE TRIGGER timestamp_trigger
BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE PROCEDURE timestamp_update();

Related

PostgreSQL Trigger update all row in the same table

I want calculate a field when update one or two fields but Trigger calculate all table's row with same value, not only the row updated. What is wrong ..? Thank you.
Create or Replace Function hospital_ocupation() returns Trigger
as
$$
Begin
update hospital set p_ocupation = (new.n_cases*100)/new.capacity
where old.n_case <> new.n_case;
return new;
End
$$
language plpgsql;
Create Trigger update_ocupation after update on hospital
for each row
WHEN (pg_trigger_depth() = 0)
execute procedure hospital_ocupation();
update hospital set capacity=500,n_cases=50
where id_hospital = 1
enter image description here
Don't use UPDATE, assign the calculated value to the new record in a BEFORE trigger:
create or replace function hospital_ocupation()
returns trigger
as
$$
begin
new.p_ocupation := (new.n_cases*100)/new.capacity;
return new;
End
$$
language plpgsql;
To make that work you need a BEFORE trigger:
create trigger update_ocupation
BEFORE update on hospital
for each row
WHEN (old.n_case <> new.n_case)
execute procedure hospital_ocupation();

Postgresql delete record involving two tables and store this record in the third record

I'm trying to delete a record from the table 'student', where on a Cascade delete it will remove it from the 'entry' table. But before delete i need to store this record in the third table 'cancel'.
Here is what i worked out so far:
DELETE FROM "CMPS".student
WHERE sno = '1';
CREATE TRIGGER canceled BEFORE DELETE
ON entry
FOR EACH ROW
EXECUTE PROCEDURE trigger_backup_row
CREATE OR REPLACE FUNCTION trigger_backup_row(integer)
RETURNS trigger AS
$$
BEGIN
INSERT INTO cancel (eno, excode, sno) values (NEW.eno, NEW.excode, NEW.sno);
RETURN NEW;
END;
$$
language PLPGSQL
But comes back with an errors. Any help will be much appreciated.
I suppose you need:
CREATE OR REPLACE FUNCTION trigger_backup_row()
RETURNS trigger AS
$$
BEGIN
INSERT INTO cancel (eno, excode, sno) values (OLD.eno, OLD.excode, OLD.sno);
RETURN OLD;
END;
$$
language PLPGSQL
;
CREATE TRIGGER canceled BEFORE DELETE
ON entry
FOR EACH ROW
EXECUTE PROCEDURE trigger_backup_row()
;
trigger function do not use arguments
on delete yo udon't have any NEW row - just an OLD one

Trigger error in postgreSQL

I am attempting to create a trigger which will delete rows when a certain where clause criteria is met but it throws out an error. What am I doing wrong?
CREATE TRIGGER unknowns
AFTER INSERT
ON "Amazon".salesdatapcr
FOR EACH ROW
EXECUTE PROCEDURE delete_my_rows();
CREATE OR REPLACE FUNCTION delete_my_rows()
RETURNS trigger AS
$BODY$
BEGIN
DELETE FROM "Amazon".salesdatapcr WHERE "Builder" = 'unknown';
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
You must encapsulate your DELETE statement in a function, so the trigger will become:
CREATE TRIGGER unknowns
AFTER INSERT
ON "Amazon".salesdatapcr
FOR EACH ROW
EXECUTE PROCEDURE delete_my_rows();
So you must create a function as follow:
CREATE OR REPLACE FUNCTION delete_my_rows()
RETURNS trigger AS
$BODY$
BEGIN
DELETE FROM "Amazon".salesdatapcr WHERE "Builder" = 'unknown';
RETURN NEW;
END;
HERE you can find a brief guide on trigger creation

How to send current schema and affected table with notification function/trigger?

When a row is inserted or updated in a specific table (in this example it's the table called 'fpl'). How can I include the affected table and schema in the notification?
SQL as follows:
CREATE TRIGGER fpl_event
AFTER INSERT OR UPDATE ON fpl
FOR EACH ROW
EXECUTE PROCEDURE fpl_notify();
CREATE OR REPLACE FUNCTION fpl_notify()
RETURNS trigger AS $$
BEGIN
NOTIFY dbNotification, 'something got insereted in fpl!';
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
Update:
CREATE OR REPLACE FUNCTION fpl_notify() RETURNS trigger
AS
$$
BEGIN
EXECUTE format('notify dbNotification, ''%s''', TG_TABLE_SCHEMA);
RETURN NULL;
END;
$$ LANGUAGE PLPGSQL;
read trigger special variables

PL/PgSQL creating a conditional trigger on insert

I'm working on my first ever Trigger. When I'm doing an INSERT on table I want to conditionaly remove rows from other table.
Here is a trigger:
CREATE OR REPLACE FUNCTION clear_seen_by()
RETURNS trigger AS
$BODY$
BEGIN
IF (OLD.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Invoked by:
CREATE TRIGGER clear_seen_by
AFTER INSERT
ON news
FOR EACH STATEMENT
EXECUTE PROCEDURE clear_seen_by();
As an error I see that NEW or OLD (if I motify the trigger) is not declared/unknown. Where is the problem?
In an INSERT statement you do not have an OLD record defined.
You should use NEW.popup instead, and also declare the trigger to be FOR EACH ROW.
CREATE OR REPLACE FUNCTION clear_seen_by() RETURNS trigger AS
$BODY$
BEGIN
IF (NEW.popup = '1') THEN
DELETE FROM news_seen_by;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
CREATE TRIGGER
clear_seen_by
AFTER INSERT ON
news
FOR EACH ROW EXECUTE PROCEDURE
clear_seen_by();
You declare a trigger FOR EACH STATEMENT. Maybe you need FOR EACH ROW?
FOR EACH STATEMENT triggers do not have NEW and OLD.