X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 28EFBDAC33 for ; Tue, 25 Oct 2005 12:24:50 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 87753-07 for ; Tue, 25 Oct 2005 15:24:47 +0000 (GMT) Received: from floppy.pyrenet.fr (news.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id C4488DA9F8 for ; Tue, 25 Oct 2005 12:24:45 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id AEECA31059; Tue, 25 Oct 2005 17:24:44 +0200 (MET DST) From: "codeWarrior" X-Newsgroups: pgsql.sql Subject: Re: automatic update or insert Date: Tue, 25 Oct 2005 08:26:16 -0700 Organization: Hub.Org Networking Services Lines: 79 Message-ID: References: <1129790184.351579.302550@g47g2000cwa.googlegroups.com> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Original To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=2.25 required=5 tests=[AWL=-0.014, FORGED_HOTMAIL_RCVD2=1.079, PRIORITY_NO_NAME=1.185] X-Spam-Level: ** X-Archive-Number: 200510/290 X-Sequence-Number: 23143 The following trigger procedure works for me.... you'd need to adjust this to manipulate YOUR table schema: DROP FUNCTION dmc_comp_plan_duplicates() CASCADE; CREATE OR REPLACE FUNCTION dmc_comp_plan_duplicates() RETURNS "trigger" AS $BODY$ DECLARE did integer; BEGIN SELECT COALESCE(id, 0) AS id FROM dmc_compensation_plan_quota WHERE dmc_compensation_plan = NEW.dmc_compensation_plan AND dmc_quota_item = NEW.dmc_quota_item INTO did; RAISE NOTICE 'DID: %', did; IF ((did = 0) OR (did IS NULL)) THEN RAISE NOTICE 'INSERT: DID: %', did; -- INSERT INTO dmc_compensation_plan_quota (dmc_compensation_plan, dmc_quota_item) VALUES (NEW.dmc_compensation_plan, NEW.dmc_quota_item); RETURN NEW; ELSE RAISE WARNING 'UPDATE: DID: %', did; UPDATE dmc_compensation_plan_quota SET active_flag = TRUE WHERE id = did; RETURN NULL; END IF; -- DEFAULT = DO NOTHING... RETURN NULL; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; ALTER FUNCTION dmc_comp_plan_duplicates() OWNER TO datamosaics; CREATE TRIGGER dmc_comp_plan_duplicates BEFORE INSERT ON dmc_compensation_plan_quota FOR EACH ROW EXECUTE PROCEDURE dmc_comp_plan_duplicates(); "tobbe" wrote in message news:1129790184.351579.302550@g47g2000cwa.googlegroups.com... > Hi. > > I have a little problem. > > In a system of mine i need to insert records into table [tbStat], and > if the records exist i need to update them instead and increase a > column [cQuantity] for every update. > > I.e. the first insert sets cQuantity to 1, and for every other run > cQuantity is increased. > > Currently i have implemented this as a stored procedure in the plpgsql > language. This means that in my stored procedure i first do a select to > find out if the row exists or not, then i do a insert or update > depending if the row existed. > > Unfortunately, stored procedures seems awfully slow. And i need the > application to go faster. > > One solution could be to implement the stored procedure in my program > instead. I think that this will be atleast 50% faster than my stored > procedure, so that would be ok. > > However, this has made me thinking. Couldn't this be done directly in > SQL? > > > Brgds Robert >