Re: SQL design pattern for a delta trigger?

From: Ted Byers <r(dot)ted(dot)byers(at)rogers(dot)com>
To: Erik Jones <erik(at)myemma(dot)com>
Cc: Vivek Khera <khera(at)kcilink(dot)com>, Colin Wetherbee <cww(at)denterprises(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: SQL design pattern for a delta trigger?
Date: 2007-12-10 23:10:03
Message-ID: 255450.21427.qm@web88306.mail.re4.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thanks Erik
>
> In a stored procedure you'd just execute the UPDATE
> and then check
> the FOUND variable to see if it found a row to
> update:
>
> UPDATE table_name SET foo='bar' WHERE id=5;
>
> IF NOT FOUND THEN
> INSERT INTO table_name (id, foo) VALUES (5, 'bar');
> END IF;
>
To be clear, if I understand you correctly, with your
example, if there is no record where id=5, nothing
happens except FOUND is set to false? Can I, then,
declare a variable prior to your update statement, and
then modify your update statement so that the value in
a particular field on the row where id=5 can be
captured? Bearing in mind this is to be in a row
level trigger after an insert into table_name,
something like:

DECLARE q DOUBLE;
UPDATE table_name
SET foo='bar',
q = table_name.quantity
WHERE id=5;

And then follow that with something like:
IF FOUND THEN
INSERT INTO another_table (baz,quantity)
VALUES (foo,q+NEW.quantity);
ELSE
INSERT INTO another_table (baz,quantity)
VALUES (foo,NEW.quantity);
END IF

Thanks again,

Ted

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Erik Jones 2007-12-10 23:11:52 Re: partitioned table query question
Previous Message Erik Jones 2007-12-10 22:57:11 Re: SQL design pattern for a delta trigger?