Received: from kiln.isn.net (root@kiln.isn.net [198.167.161.1]) by hub.org (8.9.3/8.9.3) with ESMTP id HAA15259 for ; Mon, 30 Aug 1999 07:12:28 -0400 (EDT) (envelope-from ctassell@isn.net) Received: from niki (julia04.isn.net [198.167.161.172]) by kiln.isn.net (8.9.3/8.9.3) with SMTP id IAA18380; Mon, 30 Aug 1999 08:12:04 -0300 Message-Id: <4.1.19990830074918.00a0f100@mailer.isn.net> X-Sender: ctassell@mailer.isn.net X-Mailer: QUALCOMM Windows Eudora Pro Version 4.1 Date: Mon, 30 Aug 1999 08:20:37 -0300 To: pgsql-sql@postgreSQL.org From: Charles Tassell Subject: Rules Question Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" 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.