Re: Translating Oracle CREATE TRIGGER statement

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Scott Cain <scain(at)safehooks(dot)org>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Translating Oracle CREATE TRIGGER statement
Date: 2003-04-03 19:14:59
Message-ID: 12170.1049397299@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Scott Cain <scain(at)safehooks(dot)org> writes:
> I have no experience with Oracle or with triggers in general, but I've
> been given several CREATE statements to base my Postgres schema on, but
> I don't know how to translate the trigger statements. Here is an
> example Oracle create:

> CREATE OR REPLACE TRIGGER fgroup_gid_ai
> BEFORE INSERT ON fgroup
> FOR EACH ROW WHEN (new.gid IS NULL OR new.gid = 0)
> BEGIN
> SELECT fgroup_gid_sq.nextval INTO :new.gid FROM dual;
> END;

You need to convert the action (and in this case the condition as well)
into a plpgsql function. Something like this (untested) code:

create function fgroup_insert_trig() returns trigger as '
begin
if new.gid IS NULL OR new.gid = 0 then
new.gid := nextval(''fgroup_gid_sq'');
end if;
return new;
end' language plpgsql;

create trigger fgroup_gid_ai before insert on fgroup
for each row execute procedure fgroup_insert_trig();

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Lamar Owen 2003-04-03 19:30:00 Re: [NOVICE] Postgres Syslog
Previous Message Devrim GUNDUZ 2003-04-03 18:53:06 Re: [NOVICE] Postgres Syslog