Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgWzI-0000Jx-Qf for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:47:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgWzH-0003Bs-Pi for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:47:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgWzH-0003Bk-HU for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:47:51 +0000 Received: from p3plsmtpa06-01.prod.phx3.secureserver.net ([173.201.192.102]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgWzB-0003k3-Aa for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:47:51 +0000 Received: from [192.168.1.54] ([98.38.100.21]) by :SMTPAUTH: with ESMTPSA id gWz8kWayO9fIkgWz8khrZD; Sat, 21 Nov 2020 10:47:42 -0700 X-CMAE-Analysis: v=2.4 cv=S/UcfKgP c=1 sm=1 tr=0 ts=5fb952be a=tZXnaF/PLMojNGgMD7Zm7A==:117 a=tZXnaF/PLMojNGgMD7Zm7A==:17 a=IkcTkHD0fZMA:10 a=YUbECV1Hr5TKJZjAIn0A:9 a=QEXdDO2ut3YA:10 X-SECURESERVER-ACCT: hagen@datasundae.com Subject: Re: INSERT Trigger to check for existing records To: pgsql-general@lists.postgresql.org References: <1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com> <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> <212448c5-e86b-46df-53d6-0149a0abf68e@aklaver.com> From: Hagen Finley Message-ID: <9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com> Date: Sat, 21 Nov 2020 10:47:42 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CMAE-Envelope: MS4xfJ54grf2P+nd/oR06YV+wdxDltuurghZIAP7PODhLk1fru1V90By5UX7KC77rOhGhh39E/v4SzJZdUMwErKfw9SnRbuLVec2dh8mjE5qBBYu9YrYGHAo wZFHfiJALjOA6AYCul8JbCJvqjoZKTbrcp/Hf4XevyxS+JwDcxrr9+zf8/YBC4tTOMPtpiE+3Dru1pR3XbRQaJdgRgFCMk+NcAQ= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thanks so much Adrian, I like this approach but as you indicated it doesn't actually NULL the INSERT. Could we UPDATE the existing record (per my fledgling chk UPDATE and then RETURN NULL? (More proof I don't know what I am talking about ;-). Hagen On 11/21/20 10:11 AM, Adrian Klaver wrote: > On 11/21/20 8:47 AM, Adrian Klaver wrote: >> On 11/21/20 8:20 AM, Adrian Klaver wrote: >>> On 11/21/20 8:00 AM, Hagen Finley wrote: >>>> Hello, >> >>> >>> Instead: >>> >>> IF NEW.ndealid = OLD.ndealid AND NEW.revusd = OLD.revusd >>>          AND NEW.stage = OLD.stage THEN >>>      RETURN NULL; --Will cancel INSERT >>> ELSE >>>      RETURN NEW; >>> >>> END IF; >> >> Well this is what happens when I answer BC(before coffee). The above >> will not work, if for no other reason then OLD does not exist in an >> INSERT. Will try to come up with something that is in the realm of >> possibility. > > Alright caffeine in the blood stream, so something that might actually > work: > > DECLARE >     match_ct integer; > BEGIN > >     SELECT INTO >     match_ct count(*) >     FROM >         sfdc >     WHERE >         ndealid = NEW.ndealid >     AND >         revusd = NEW.revusd >     AND >        stage = NEW.stage; > >    IF match_ct > 0 THEN >        RETURN NULL; --Will cancel INSERT >    ELSE >        RETURN NEW; >    END IF; > > END; > > Though I would also point you at David's solution. Given that you are > only looking at ~20% of the records being different it would save you > a lot of churning through INSERTs. > >> >>> >>>> >>>> Hagen >>>> >>>> Larimer County, CO >>>> >>> >>> >> >> > >