Re: unique value - trigger?

From: Dmitry Tkach <dmitry(at)openratings(dot)com>
To: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: unique value - trigger?
Date: 2003-07-17 16:03:34
Message-ID: 3F16C8D6.1050603@openratings.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>
>
>The problem is I don't know how to convert the following pseudo code to valid
>SQL:
>
>create trigger unique_current on insert/update to lnumbers
> if new.lncurrent = true
> update lnumbers set all other records for this loco to false
>
>
>
I see... The bad news is you can't do it directly... You can only create
functions to run as triggers, not plain sql statements for some reason :-(
The correct syntax is

create trigger unique_current before insert or update on lnumbers for
each row execute procedure reset_current_lnum();

Where reset_current_lnum () is a function, that you have to write either
in "C" or in 'plpgsql';
I could give you some sample code in "C" to do that, but it's rather
complicated if you are not used to writing postgres stored procs in C...
plpgsql would be much easier, but I can't help you there, because I
don't know the syntax ... something like this, I guess, but I doubt this
will compile exactly as it is:

create function reset_current_lnum () returns triggers as
'
begin
if new.lncurrent = true
update lnumbers set lncurrent=false where lnid=new.lnid and
lncurrent;
endif
return new;
end;' language 'plpgsql';

Dima

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Poole 2003-07-17 16:05:47 Re: unique value - trigger?
Previous Message Gary Stainburn 2003-07-17 15:49:25 Re: unique value - trigger?