creating a trigger to write to a different table

Started by Peter Choeabout 25 years ago2 messagesgeneral
Jump to latest
#1Peter Choe
choepete@mindspring.com

can i use pl/pgsql to write a trigger to update a separate table?

for example if i have a table student:
name text,
id text,
group int,
stat int,

and another table status:
stat text
group int
count int

and i update the stat in the student table, can i update the count
column in the status table to reflect the change. in the documents, i
just see triggers used on one table.

peter choe

#2Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Peter Choe (#1)
Re: creating a trigger to write to a different table

On Thu, 22 Mar 2001, Peter Choe wrote:

can i use pl/pgsql to write a trigger to update a separate table?

You should be able to. You can use update, etc, from the trigger.

For example a simple summing one might be (not really tested)
create function summer() returns opaque
as '
begin;
update table2 set val=val+NEW.val where id=NEW.id;
return NEW;
end;
' language 'plpgsql';
)

create trigger tr after insert on table1 for each row execute
procedure summer();