user262503
user262503

Reputation: 103

Execute stored procedure based on conditions postgres

I have a code like this :

create trigger audit_trigg_Test After insert on LOG 
for each row when(new.j='PR' and new.ds='FB') execute procedure ABC()
     when(new.j='PR' and new.ds='LNK')  execute procedure XYZ()


create or replace function ABC() returns trigger as $$
begin
--Stuff
end

create or replace function XYZ() returns trigger as $$
begin
--Stuff
end

The trigger should work on stored procedures based on the conditions

when(new.j='PR' and new.ds='FB') execute procedure ABC()
         when(new.j='PR' and new.ds='LNK')  execute procedure XYZ()

How can I do this?

Upvotes: 1

Views: 952

Answers (1)

Patrick
Patrick

Reputation: 32234

For this you need to define two separate triggers:

CREATE TRIGGER audit_trigg_Test_ABC
AFTER INSERT ON log 
FOR EACH ROW WHEN (NEW.j = 'PR' AND NEW.ds = 'FB') EXECUTE PROCEDURE ABC();

CREATE TRIGGER audit_trigg_Test_XYZ
AFTER INSERT ON log 
FOR EACH ROW WHEN (NEW.j = 'PR' AND NEW.ds = 'LNK') EXECUTE PROCEDURE XYZ();

You can use the WHEN clause to determine if a trigger is fired, but you cannot conditionally select a trigger function to call.

Upvotes: 1

Related Questions