pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hagen Finley <hagen@datasundae.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: INSERT Trigger to check for existing records
Date: Sat, 21 Nov 2020 10:47:42 -0700
Message-ID: <9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com> (raw)
In-Reply-To: <b997557b-838c-87fe-585f-1fc140109668@aklaver.com>
References: <CAKoxK+5x9J74Sn=rvGSu4dAkSdpF=9AoTjs9-KmnSGd6c03SxQ@mail.gmail.com>
	<1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com>
	<CAKoxK+4DGFeevYrCJ3oEWxqXGodtEKt33dpQQjvw0O1ZwMgWyA@mail.gmail.com>
	<3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com>
	<fea79107-d23b-2725-be21-ee487031843d@aklaver.com>
	<212448c5-e86b-46df-53d6-0149a0abf68e@aklaver.com>
	<b997557b-838c-87fe-585f-1fc140109668@aklaver.com>

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
>>>>
>>>
>>>
>>
>>
>
>





view thread (22+ messages)  latest in thread

Message-ID: <9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com>
Permalink:  ../9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com/
Also on:    postgresql.org/message-id/9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hagen@datasundae.com
  Subject: Re: INSERT Trigger to check for existing records
  In-Reply-To: <9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com>
  References: <CAKoxK+5x9J74Sn=rvGSu4dAkSdpF=9AoTjs9-KmnSGd6c03SxQ@mail.gmail.com> <1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com> <CAKoxK+4DGFeevYrCJ3oEWxqXGodtEKt33dpQQjvw0O1ZwMgWyA@mail.gmail.com> <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> <fea79107-d23b-2725-be21-ee487031843d@aklaver.com> <212448c5-e86b-46df-53d6-0149a0abf68e@aklaver.com> <b997557b-838c-87fe-585f-1fc140109668@aklaver.com> <9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox