Re: unique value - trigger? - Mailing list pgsql-sql

From Dmitry Tkach
Subject Re: unique value - trigger?
Date
Msg-id 3F16C8D6.1050603@openratings.com
Whole thread Raw
In response to Re: unique value - trigger?  (Gary Stainburn <gary.stainburn@ringways.co.uk>)
List 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





pgsql-sql by date:

Previous
From: Gary Stainburn
Date:
Subject: Re: unique value - trigger?
Next
From: Richard Poole
Date:
Subject: Re: unique value - trigger?