Rules Question

From: Charles Tassell <ctassell(at)isn(dot)net>
To: pgsql-sql(at)postgreSQL(dot)org
Subject: Rules Question
Date: 1999-08-30 11:20:37
Message-ID: 4.1.19990830074918.00a0f100@mailer.isn.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have a question on using INSERT rules. What I am attempting to do is to
create a rule that checks to see if the item being inserted already exists.
If it does exist, increase the stored quantity bye the NEW.quantity,
otherwise, just handle the INSERT as normal.

Here is what I've tried

CREATE TABLE orders (
cust_id int,
item_id text,
quantity int
);

CREATE RULE combine_dupes AS ON INSERT TO orders
WHERE NEW.item_id IN
(SELECT item_id FROM orders
WHERE cust_id = NEW.cust_id AND item_id = NEW.item_id)
DO
UPDATE orders SET quantity = NEW.quantity + quantity
WHERE cart_id = NEW.cart_id AND item_ID = NEW.item_id;

I've also tried it as a DO INSTEAD and without the "AND item_id =
NEW.item_id" clause (which shouldn't be needed) but they don't work either.

So, what's the deal? Am I making a syntax error, or do I need to use a
function or trigger in order to do what I'm trying?

BTW: Please respond or cc directly to me, I only subscribe to
pgsql-general, I just thought this was more SQL related.

Browse pgsql-sql by date

  From Date Subject
Next Message Holm Tiffe 1999-08-30 12:04:52 problem with select
Previous Message Tom Lane 1999-08-29 17:02:32 Re: [SQL] SELECT multiple tables with same fields