Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 492ED6501B9 for ; Wed, 3 Sep 2008 19:00:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 31488-01-9 for ; Wed, 3 Sep 2008 19:00:19 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 4E5596502C5 for ; Wed, 3 Sep 2008 18:59:38 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id DF77B36438; Wed, 3 Sep 2008 14:59:35 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id DCCE0363FD; Wed, 3 Sep 2008 14:59:35 -0700 (PDT) Date: Wed, 3 Sep 2008 14:59:35 -0700 (PDT) From: Stephan Szabo To: Mira Dimitrijevic Cc: pgsql-general@postgresql.org Subject: Re: Problem with trigger function In-Reply-To: <3936E50E-763C-4536-9B42-0D68B57902E4@lanl.gov> Message-ID: <20080903145625.G39121@megazone.bigpanda.com> References: <3936E50E-763C-4536-9B42-0D68B57902E4@lanl.gov> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200809/133 X-Sequence-Number: 137140 On Wed, 3 Sep 2008, Mira Dimitrijevic wrote: > Hi, > I wrote the trigger function below and when trying to execute it, I > get the following error: > > 15:00:42 [CREATE - 0 row(s), 0.000 secs] [Error Code: 0, SQL > State: 42601] ERROR: syntax error at or near "INSERT" > > I am using DBVisualizer's "SQL Commander" window, not the "create > funciton" option. I know the SQL is correct - I can execute it just > by itself. The problem is somewhere in the > syntax for a postgres trigger function. Any input on what the problem > might be would be just fabulous! It looks like you're not always quoting properly if the below is an exact quote. For example you double the single quotes in the IF tg_op = ''INSERT'' but don't double them in the VALUES ('INSERT'). You might find it easier to use dollar quoting for the function body (using $$ instead of ' to wrap the body) if you're using a version that supports it which will mean you shouldn't need to double any of the quotes inside the function. > CREATE OR REPLACE FUNCTION audit_sequence_update() RETURNS trigger AS ' > BEGIN > IF tg_op = ''INSERT'' THEN > INSERT INTO audit_sequence_update(operation, "day", > owner_uid, "sequence") > VALUES > ('INSERT', NEW."day", NEW.owner_uid, NEW."sequence"); > ELSE > INSERT INTO audit_sequence_update(operation, "day", > owner_uid, "sequence") > SELECT > 'UPDATE', "day", owner_uid, "sequence" > FROM sequence where isdid=NEW.isdid; > END IF; > RETURN NEW; > END > ' LANGUAGE plpgsql;