Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 7F8819FB57E for ; Wed, 25 Jul 2007 19:29:25 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 51904-09 for ; Wed, 25 Jul 2007 19:29:21 -0300 (ADT) X-Greylist: whitelisted by SQLgrey-1.7.5 Received: from mail16.syd.optusnet.com.au (mail16.syd.optusnet.com.au [211.29.132.197]) by postgresql.org (Postfix) with ESMTP id 0EA3A9FB53D for ; Wed, 25 Jul 2007 19:29:19 -0300 (ADT) Received: from [127.0.0.1] (online4-new.lnk.telstra.net [139.130.169.227]) (authenticated sender plengada@optusnet.com.au) by mail16.syd.optusnet.com.au (8.13.1/8.13.1) with ESMTP id l6PMTErV016084 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 26 Jul 2007 08:29:16 +1000 Message-ID: <46A7CEB6.90607@autoledgers.com.au> Date: Thu, 26 Jul 2007 06:29:10 +0800 From: Paul Lambert User-Agent: Thunderbird 2.0.0.5 (Windows/20070716) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Assistance with a trigger 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: 200707/147 X-Sequence-Number: 28882 I have some triggers in an MS SQL server database which I need to copy across to a PG database. I've not yet done triggers in PG so I was hoping to get a little bit of a pointer on the first one as a place to start and work my way through from there. The trigger looks like thus: CREATE TRIGGER update_so_tran ON parts_purchasing FOR INSERT, UPDATE AS begin declare @found int declare @update varchar(10) declare @dealerid varchar(6) declare @trx_address varchar(10) declare @quan_received numeric(14,4) declare @amn_received numeric(14,4) declare @quan_invoiced numeric(14,4) declare @amn_invoiced numeric(14,4) declare @line_no int select @update = tran_status from inserted if @update = "U" or @update = "D" select @dealerid = dealer_id from inserted select @trx_address = so_tran_address from inserted select @quan_received = qty_received from inserted select @amn_received = amt_received from inserted select @quan_invoiced = qty_invoiced from inserted select @amn_invoiced = amt_invoiced from inserted select @line_no = line_number from inserted update parts_purchasing set qty_received=qty_received + @quan_received, qty_invoiced = qty_invoiced + @quan_invoiced, amt_invoiced = amt_invoiced + @amn_invoiced, amt_received = amt_received + @amn_received where dealer_id = @dealerid AND so_tran_address = @trx_address; end if end Basically in words, I have a parts_purchasing table which contains master order records as well as sub-records for the actual receival and invoicing of the orders. whenever a receival and invoice transaction comes through I need to update the qty_received, qty_invoiced, amt_received and amt_invoiced on the master record. It's pretty straight forward in SQL server but I'm having a little bit of difficulty in wrapping my head around it in PG. I know I have to create a function and call that from the trigger I'm just having a small bit of trouble in working out how to write this function. If anyone could give me a starting point that would be highly appreciated. Cheers, Paul. -- Paul Lambert Database Administrator AutoLedgers