X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 030C9D1B454 for ; Wed, 26 Nov 2003 11:52:12 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 31656-07 for ; Wed, 26 Nov 2003 07:51:43 -0400 (AST) Received: from hotmail.com (bay9-f11.bay9.hotmail.com [64.4.47.11]) by svr1.postgresql.org (Postfix) with ESMTP id 01486D1B487 for ; Wed, 26 Nov 2003 07:51:39 -0400 (AST) Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Wed, 26 Nov 2003 03:51:42 -0800 Received: from 134.29.51.143 by by9fd.bay9.hotmail.msn.com with HTTP; Wed, 26 Nov 2003 11:51:42 GMT X-Originating-IP: [134.29.51.143] X-Originating-Email: [cstotes@hotmail.com] Reply-To: cstotesbery@acm.org From: "Clint Stotesbery" To: pgsql-sql@postgresql.org Subject: trigger conversion advice needed Date: Wed, 26 Nov 2003 11:51:42 +0000 Mime-Version: 1.0 Content-Type: text/plain; format=flowed Message-ID: X-OriginalArrivalTime: 26 Nov 2003 11:51:42.0695 (UTC) FILETIME=[A8DD2770:01C3B413] X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/289 X-Sequence-Number: 16104 I'm working on converting a simple trigger from Oracle to Postgres and I have a couple ofl questions that I need some help on please. First here's the Oracle trigger: CREATE OR REPLACE TRIGGER t_ship_date AFTER UPDATE OR INSERT OF order_date ON orders BEGIN UPDATE orders SET ship_date = working_5days(order_date); END; / When I was working on converting the trigger I noticed that Postgres doesn't have the OF table_attribute ON syntax support. I just want the trigger to fire when the order_date field in the order table is updated or inserted like it is specified in the Oracle trigger specification above. So I did this in Postgres: CREATE OR REPLACE FUNCTION t_ship_date() RETURNS TRIGGER AS ' BEGIN UPDATE orders SET ship_date = working_5days(new.order_date); RETURN NEW; END; ' LANGUAGE 'plpgsql'; I justed used new.order_date in the Postgres one version. I'm pretty sure that this isn't going to work, it will probably update every ship_date which is not what I want. I just want to update the ship_date when the record's order_date is updated. Is there some way in Postgres to specify a trigger to fire only when a certain field in a table is changed or inserted (like I had in the Oracle version)? I couldn't find anything for triggers to do that in the docs so I hope someone can shed some light on this for me. I thought maybe something like this could work too (probably closer to being correct): CREATE OR REPLACE FUNCTION t_ship_date() RETURNS TRIGGER AS ' BEGIN IF TG_OP = ''INSERT'' THEN UPDATE orders SET ship_date = working_5days(new.order_date) where order_no = new.order_no; ELSIF TG_OP =''UPDATE'' THEN UPDATE orders SET ship_date = working_5days(new.order_date) where order_no = old.order_no; END IF; RETURN NEW; END; ' LANGUAGE 'plpgsql'; The working_5days function just adds 5 business days to a date fyi. Now the second question I have is due to an error I keep getting when I try and make the trigger definition below: CREATE TRIGGER t_ship_date AFTER UPDATE OR INSERT ON orders EXECUTE PROCEDURE t_ship_date(); I always get a parse error at or near execute. What am I doing wrong? If I could get the trigger to compile then I could test it to see if it works the same as the Oracle version. I wouldn't even have to ask the first question if I could get it to compile but I thought I might as well ask anyway so I don't have to post another message. Thanks for the help. -Clint P.S. 7.3.4 is the postgres version on the server. _________________________________________________________________ Has one of the new viruses infected your computer? Find out with a FREE online computer virus scan from McAfee. Take the FreeScan now! http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963