Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 85E019FB7B8 for ; Wed, 4 Apr 2007 03:19:56 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 89166-07 for ; Wed, 4 Apr 2007 03:19:46 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from rrmailscan3.safenetbox.biz (unknown [203.25.40.75]) by postgresql.org (Postfix) with ESMTP id 607B09FA521 for ; Wed, 4 Apr 2007 03:19:45 -0300 (ADT) Received: from rrmailscan3.safenetbox.biz (rrmailscan3.safenetbox.biz [127.0.0.1]) by rrmailscan3.safenetbox.biz (Postfix) with ESMTP id 8B9051D4843A for ; Wed, 4 Apr 2007 16:19:43 +1000 (EST) Received: from [127.0.0.1] (online4-new.lnk.telstra.net [139.130.169.227]) (Authenticated sender: remote) by rrmailscan3.safenetbox.biz (Postfix) with ESMTP id 81E661D4845A for ; Wed, 4 Apr 2007 16:19:42 +1000 (EST) Message-ID: <46134373.40504@autoledgers.com.au> Date: Wed, 04 Apr 2007 14:19:31 +0800 From: Paul Lambert User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Moving a simple function to pl/pgsql (Novice question) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/49 X-Sequence-Number: 28049 Forgive me in advance for this terribly novice question... I have the following function which was written in MS SQL Servers trigger/rule language... CREATE TRIGGER [Sync_Deals] ON [dbo].[Deals] FOR INSERT, UPDATE AS begin declare @Found int declare @deal varchar(10) select @deal = deal_address from inserted Select @Found = count(*) from dealbook.dbo.deals where deal_address = @deal if @Found > 0 delete from dealbook.dbo.deals where deal_address = @deal insert into dealbook.dbo.deals select * from inserted end The purpose being when a row in a table in one database is updated, it will copy (or replicate I guess) the record into a different table into another database in the same server. (deleting said record first if it already exists) What is the best way to do this within Postgres? I assume a trigger is the way to go here as well, but having not written a trigger in PG I'm a little stuck as to where to start. My client goes live with their new system on Tuesday and this function forms part of some custom additions they wrote on their previous SQL server database. I'd thus like to make sure I know what I'm doing to finalise the conversion before the weekend. It seems to me I need to create a function to do the copy, and then create a trigger to call the function - but I'm not entirely sure - you can probably tell I haven't done anything with triggers, functions or rules yet. :-) (BTW: No I am not trying to get someone to do my work for me :-P - I have about 30ish triggers of various nature to convert, I just need somewhere to start so I can figure out how it's done then I can do the rest myself) TIA, P. -- Paul Lambert Database Administrator AutoLedgers