Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 030FB1337B67 for ; Fri, 6 May 2011 14:14:24 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 02691-06 for ; Fri, 6 May 2011 17:14:16 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from smtp4-g21.free.fr (smtp4-g21.free.fr [212.27.42.4]) by mail.postgresql.org (Postfix) with ESMTP id D16C61337999 for ; Fri, 6 May 2011 14:14:13 -0300 (ADT) Received: from [192.168.0.14] (unknown [78.237.44.8]) by smtp4-g21.free.fr (Postfix) with ESMTP id A70884C80DE for ; Fri, 6 May 2011 19:14:08 +0200 (CEST) Message-ID: <4DC42C5B.1000007@club-internet.fr> Date: Fri, 06 May 2011 19:14:03 +0200 From: =?ISO-8859-1?Q?Fr=E9d=E9ric_BROUARD?= User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: FOR EACH STATEMENT trigger ? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.891 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RFC_ABUSE_POST=0.001, UPPERCASE_50_75=0.008 X-Spam-Level: X-Archive-Number: 201105/46 X-Sequence-Number: 35330 Hi there I am trying to get an example of SET BASED trigger logic with FOR EACH STATEMENT, but I cannot find any example involving the pseudo table NEW (or OLD) in the trigger function SQL statement. Let me give you a real life example. Suppose we have the above table : CREATE TABLE T_PRODUIT_DISPO_PDD (PRD_ID INT NOT NULL, PDD_BEGIN DATE NOT NULL, PDD_END DATE, PDD_QUANTITY FLOAT NOT NULL); We want to never have more thant one PDD_END = NULL for the same PRD_ID. The assertion we can do is : ALTER TABLE T_PRODUIT_DISPO_PDD ADD CONSTRAINT CK_PDD_PRD_FIN_UNIQUENULL CHECK (NOT EXISTS(SELECT 0 FROM T_PRODUIT_DISPO_PDD WHERE PDD_FIN IS NULL GROUP BY PRD_ID HAVING COUNT(*) > 1)) Which is not supported by PG So I wuld like to do this with a FOR EACH STATEMENT trigger and not by a FOR EACH ROW. Here is the code I try : CREATE OR REPLACE FUNCTION F_UNIQUE_NULL_PRD_END() RETURNS trigger AS $code$ DECLARE n_rows integer; BEGIN SELECT COUNT(*) INTO n_rows WHERE EXISTS(SELECT 0 FROM T_PRODUIT_DISPO_PDD WHERE PRD_ID IN(SELECT NEW.PRD_ID FROM NEW) AS T AND PDD_END IS NULL GROUP BY PRD_ID HAVING COUNT(*) > 1); IF ( n_rows IS NOT NULL ) THEN RAISE EXCEPTION 'Violation de la contrainte d''unicité sur le couple de colonne PRD_ID + PDD_FIN'; ROLLBACK TRANSACTION; END IF; RETURN NULL; END $code$ LANGUAGE 'plpgsql' VOLATILE Which produce an error ! Of course I can do that with a FOR EACH STATEMENT like this one : CREATE OR REPLACE FUNCTION F_UNIQUE_NULL_PRD_FIN() RETURNS trigger AS $code$ DECLARE n_rows integer; BEGIN SELECT 1 INTO n_rows WHERE EXISTS(SELECT 0 FROM T_PRODUIT_DISPO_PDD WHERE PRD_ID = NEW.PRD_ID AND PDD_FIN IS NULL GROUP BY PRD_ID HAVING COUNT(*) > 1); IF ( n_rows IS NOT NULL ) THEN RAISE EXCEPTION 'Violation de la contrainte d''unicité sur le couple de colonne PRD_ID + PDD_FIN'; ROLLBACK TRANSACTION; END IF; RETURN NULL; END $code$ LANGUAGE 'plpgsql' VOLATILE CREATE TRIGGER E_IU_PRD AFTER INSERT OR UPDATE ON T_PRODUIT_DISPO_PDD FOR EACH ROW EXECUTE PROCEDURE F_UNIQUE_NULL_PRD_FIN(); But it is absolutly not that I Want !!!! Thanks -- Frédéric BROUARD - expert SGBDR et SQL - MVP SQL Server - 06 11 86 40 66 Le site sur le langage SQL et les SGBDR : http://sqlpro.developpez.com Enseignant Arts & Métiers PACA, ISEN Toulon et CESI/EXIA Aix en Provence Audit, conseil, expertise, formation, modélisation, tuning, optimisation *********************** http://www.sqlspot.com *************************