Hello all - I apologize for the newbie-esque question, but the debug output
from postgres when you have a bug in your PL/pgSQL procedure is none to
detailed.

I've created the following procedure and am getting an error when I try to
update the table. The error is something like "parse error near ; on line 50".
Line 50 is the last line.

There's probably something glaring wrong in here that I'm not seeing, but any
help would be appreciated.

I don't know if the \ at the end of the line is a problem, but those were
added late in the game and didn't change the error message ;-)

Tim

CREATE FUNCTION artifactgroup_update_agg () RETURNS OPAQUE AS '
BEGIN
   --
   -- see if they are moving to a new artifacttype
   -- if so, its a more complex operation
   --
   IF NEW.group_artifact_id <> OLD.group_artifact_id THEN
      --
      -- transferred artifacts always have a status of 1
      -- so we will increment the new artifacttypes sums
      --
      UPDATE artifact_counts_agg SET count=count+1, open_count=open_count+1 \
         WHERE group_artifact_id=NEW.group_artifact_id;

      --
      --      now see how to increment/decrement the old types sums
      --
      IF NEW.status_id <> OLD.status_id THEN 
         IF OLD.status_id = 2 THEN
            UPDATE artifact_counts_agg SET count=count-1 \
               WHERE group_artifact_id=OLD.group_artifact_id;
         --
         --      no need to do anything if it was in deleted status
         --
         END IF;
      ELSE
         --
         --      Was already in open status before
         --
         UPDATE artifact_counts_agg SET count=count-1, open_count=open_count-1 \
            WHERE group_artifact_id=OLD.group_artifact_id;
      END IF;
   ELSE
      --
      -- just need to evaluate the status flag and 
      -- increment/decrement the counter as necessary
      --
      IF NEW.status_id <> OLD.status_id THEN
         IF new.status_id = 1 THEN
            UPDATE artifact_counts_agg SET open_count=open_count+1 \
               WHERE group_artifact_id=new.group_artifact_id;
         ELSE IF new.status_id = 2 THEN
            UPDATE artifact_counts_agg SET open_count=open_count-1 \
               WHERE group_artifact_id=new.group_artifact_id;
         ELSE IF new.status_id = 3 THEN
            UPDATE artifact_counts_agg SET open_count=open_count-1,count=count-1 \
               WHERE group_artifact_id=new.group_artifact_id;
         END IF;
      END IF; 
   END IF;
   RETURN NEW;
END;
' LANGUAGE 'plpgsql';

CREATE TRIGGER artifactgroup_update_trig AFTER UPDATE ON artifact
   FOR EACH ROW EXECUTE PROCEDURE artifactgroup_update_agg();


-- 
Founder - PHPBuilder.com / Geocrawler.com
Lead Developer - SourceForge
VA Linux Systems

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

Reply via email to