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 1khxt9-0006e8-Rm for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 16:43:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khxt8-0006SJ-Rr for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 16:43:26 +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 1khxt8-0006SC-KW for pgsql-general@lists.postgresql.org; Wed, 25 Nov 2020 16:43:26 +0000 Received: from p3plsmtpa06-04.prod.phx3.secureserver.net ([173.201.192.105]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khxt6-0008Ix-Kf for pgsql-general@lists.postgresql.org; Wed, 25 Nov 2020 16:43:26 +0000 Received: from DESKTOPPBOKT6J ([98.38.100.21]) by :SMTPAUTH: with ESMTPSA id hxt3krSIT5yf9hxt3k3xIM; Wed, 25 Nov 2020 09:43:22 -0700 X-CMAE-Analysis: v=2.4 cv=Hsqzp2fS c=1 sm=1 tr=0 ts=5fbe89aa a=tZXnaF/PLMojNGgMD7Zm7A==:117 a=tZXnaF/PLMojNGgMD7Zm7A==:17 a=IkcTkHD0fZMA:10 a=SNL7wKpgAAAA:8 a=C-sdMEEzAAAA:8 a=epTmVMiNAAAA:8 a=5GSxhIcTGOxbOOuqKoQA:9 a=QEXdDO2ut3YA:10 a=uEXi9pYaZPHPrIM342IW:22 a=7WAne9lm5b0o3WrbkWlm:22 a=ndEWmUVY6Yapc0oHF_P4:22 X-SECURESERVER-ACCT: hagen@datasundae.com From: To: "'Adrian Klaver'" , References: <1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com> <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> <000e01d6c036$fda479b0$f8ed6d10$@datasundae.com> <2ecce3f7-f298-7fd2-f470-5cf9a71b349c@datasundae.com> <4131ec0f-7dfa-d170-8312-d4fa3fc7ec0f@aklaver.com> <204a1ad6-fd56-fa19-2211-cde267810b92@datasundae.com> In-Reply-To: Subject: RE: INSERT Trigger to check for existing records : Does this do what I hope it is doing? Date: Wed, 25 Nov 2020 09:43:20 -0700 Message-ID: <07cf01d6c34a$157bf610$4073e230$@datasundae.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQI1vRxzwZPQCHZ4ohRSJ/ouoa4aowLrdCJXAWXtZU8CkAIwLwMR34T1Ak2bFosC2ze7PAHLl6oSAdwS6DQCPNrjmwFOSW2KAulG9xECigUmXwHkH2ttAVPWwiKoI0ABoA== Content-Language: en-us X-CMAE-Envelope: MS4xfKH2xoakdCZMOq/HELpRlXNsWzhszMOSHTQgnQ3mECH/cnxjEIJfT3l0MoIA56K+sPfvb99BPWu1XzCCMRjUJzx4X/I4dgArFZ/OyjbPn96m7EshkXV2 eRK9gCWn185rKMLVR6EbiqR6SsOeLFJ24Yv2ZUKN3/gYgjt7v7b+aIJcRq38KpDqJA7rKHUPdOBscFmllA9I4OL1KYruCK1B8zudiMfaVDmWbVVSGW/Zf0Cr oRJyGv3tlmn3HcxK2bGcSA== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Adrian, Thanks for your detailed response. That's very kind and much = appreciated. 1. OK that's just me groping for a RETURN statement that doesn't throw a = rod. I don't actually need to return anything as the goal of the = FUNCTION (for the moment) is to perform updates to a table. It might be = nice to return some sort of confirmation but it's not necessary. = Apparently I don't fully understand the RETURN concept (oh really? = =F0=9F=98=89. Any suggestions where to research or read? 2. I have two tables: a) sfdc which is the baseline - doesn't change - isn't updated by this = FUNTION b) hygiene_119 a new table which has some records (~80%) which are = identical to those already in sfdc.=20 The logic flow is: i) SELECT the dealids from hygiene_119 (latest or new report dated = 11/9) ii) compare those hygiene_119.dealids with the existing sfdc.dealids = - hence the IF $1 (one result from the hygiene_119.dealdid SELECT) is = IN (matches) any of the sfdc.dealids THEN iii) UPDATE hygiene_119 SET status =3D 'SAME' WHERE dealid =3D $1; = --flag that hygiene_119 record as the SAME or a duplicate record iv) ELSE UPDATE hygiene_119 SET status =3D 'NEW' WHERE dealid =3D $1; = --flag that hygiene_119 record as NEW or a new record Once I have inspected the "NEW" records in hygiene_119 I will INSERT = then into sfdc. Then rinse and repeat each week with a new report. 3. Not positive the IF is doing what I want, but if I copy a = sfdc.dealid into the same_test() parameter field the FUNTION does update = the hygiene_119.status field properly. To me, it appears I just need a = way to iterate through and insert one hygiene_119.dealid in the = same_test parameter field. Then the UPDATE should flag all the = hygiene_119 records as SAME or NEW. Obviously I don't REALLY need both = flags as the absence of a flag would indicate status too. Does that articulate the thought process adequately? Best, Hagen -----Original Message----- From: Adrian Klaver =20 Sent: Wednesday, November 25, 2020 9:07 AM To: Hagen Finley ; = pgsql-general@lists.postgresql.org Subject: Re: INSERT Trigger to check for existing records : Does this do = what I hope it is doing? On 11/25/20 7:41 AM, Hagen Finley wrote: > Folks, >=20 > Just a quick question. *Using this FUNCTION:* >=20 > CREATE OR REPLACE FUNCTION same_test(did numeric) > RETURNS numeric AS $$ > BEGIN > IF $1 IN > (SELECT dealid from sfdc) > THEN > UPDATE hygiene_119 SET status =3D 'SAME'; > ELSE > UPDATE hygiene_119 SET status =3D 'NEW'; > END IF; > RETURN NULL; > END; > $$ LANGUAGE plpgsql; The above is broken in multiple ways: 1) You have RETURNS numeric and then RETURN NULL; This means you will = not actually return anything 2) You have the input argument did but you never use it to restrict your = UPDATEs. 3) Not sure the logic in the IF actually works even if you filtered by = did. This assumes that there will always be a row in hygiene_119 that = matches one in hygiene_112. Given that you setting a 'NEW' flag I'm = guessing that is not the case. You will need to sketch out the thought process at work here before we = can go any further with this. >=20 > *Does the following query input the the dealids that result from the=20 > SELECT statement into the parameter of the sames_test() FUNCTION?* >=20 > Select dealid sametest(dealid) FROM hygiene_123; Have no idea what that is supposed to do? If you want to use the function(after fixing it) you would have to do: select * from some_test(some_number); >=20 > I doubt it does (my query runs a /long time)/ :-). I know I can = utilize=20 > python to push SELECT results into a array and then run a 'FOR d in=20 > dealids' LOOP to feed the FUNCTION parameter but I'd like to learn how = > to do that with nested SQL statements or FUNCTIONS. >=20 > Thanks! >=20 >=20 > Hagen >=20 --=20 Adrian Klaver adrian.klaver@aklaver.com