X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.225.181]) by svr1.postgresql.org (Postfix) with ESMTP id D021FCCA51 for ; Thu, 17 Jul 2003 16:02:46 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr5.postgresql.org [64.117.225.181]) (amavisd-new, port 10024) with ESMTP id 56385-04 for ; Thu, 17 Jul 2003 16:03:06 +0000 (GMT) Received: from exchange.hq.openratings.com (unknown [141.149.165.226]) by svr1.postgresql.org (Postfix) with ESMTP id 88200CCA49 for ; Thu, 17 Jul 2003 13:02:35 -0300 (ADT) Received: from openratings.com (cancun.hq.openratings.com [10.0.0.225]) by exchange.hq.openratings.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id 38QM44VK; Thu, 17 Jul 2003 12:03:05 -0400 Message-ID: <3F16C8D6.1050603@openratings.com> Date: Thu, 17 Jul 2003 12:03:34 -0400 From: Dmitry Tkach User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Gary Stainburn Cc: pgsql-sql@postgresql.org Subject: Re: unique value - trigger? References: <200307171256.52098.gary.stainburn@ringways.co.uk> <3F16B3EE.2060006@openratings.com> <200307171649.25614.gary.stainburn@ringways.co.uk> In-Reply-To: <200307171649.25614.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/229 X-Sequence-Number: 14343 > > >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