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 1khxKF-00046J-4X for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 16:07:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khxJg-0004FR-SO for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 16:06:48 +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 1khxJf-0004FG-FO for pgsql-general@lists.postgresql.org; Wed, 25 Nov 2020 16:06:48 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khxJX-00081W-JE for pgsql-general@lists.postgresql.org; Wed, 25 Nov 2020 16:06:47 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 455916BF; Wed, 25 Nov 2020 11:06:35 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Wed, 25 Nov 2020 11:06:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=S +Azibb5i08OtqGq/ykiwlB/3mauVg1QiDWc6mdIK7E=; b=altkrtSJBSQnHeCD7 GIizqrbINy2GYbdSgmaPAcn9I/H02LAcvMZZyTUfr8EoJb2hPt3xIqzmngUKUO12 M1im70xiGnZJ5uTatXsnhkTlmETEv169bLGDMW0/HlXOU8NCrh6J3oF8J1vmAfl6 B0KSCx0o01X6RhSbKm2VIEthEI+c9DLJmm1k+gRatw5Q4/9dMJBEWTEGzLC/LHKa ITwwouUNkP4OGEXTrJcRZ3b3t0MTfKmt3VSYk4bzw59byNKtcefc71+5K6cxzv2p RqMwWkMSOz2SGHE635oxsi5Ey3HaoYWaNcnBOFyBtjNbMMIDbIAADSeXZfiZaXjz uvTKw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=S+Azibb5i08OtqGq/ykiwlB/3mauVg1QiDWc6mdIK 7E=; b=kJHEPLAuTEccbJYU54ajTiKAr6vGsCiT3s4qX3rQAMP4qkYV1EPcrn12a HAD8gIrtXbozOwalz9zF21eY/XmF+CxiBd5OurHJ61sWMlzcocirN+yynKGMElBX z7U6jRVl8Z0L2AfgUYH+ZZXWMrYuosKHKLF2QwdidE9kFc2lPTMuUhdcYhIfRuNx WyPOLpvt/Up44qcSkYxB+DmGbKx5jEpnxyjg3iRbEaJAKHOC4F2V2EBC76orpSO+ Hbet+tz7etUxckXWCoTlhsuxQijKt1CMR+DL/jvZdrZMjZlNnMKeXObs0rQec9s2 QU9aZKF87e0STSfDTWeKPldZJ2BUQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudehtddgkeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeevveefveefueegffekkefhgeeiueffgfetueejvdfg hfeujeejhfekjeelgedtgeenucfkphepjeehrddujedvrdduledrudefleenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-19-139.tukw.qwest.net [75.172.19.139]) by mail.messagingengine.com (Postfix) with ESMTPA id 15BFA3064AA6; Wed, 25 Nov 2020 11:06:33 -0500 (EST) Subject: Re: INSERT Trigger to check for existing records : Does this do what I hope it is doing? To: Hagen Finley , pgsql-general@lists.postgresql.org 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> From: Adrian Klaver Message-ID: Date: Wed, 25 Nov 2020 08:06:33 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/25/20 7:41 AM, Hagen Finley wrote: > Folks, > > Just a quick question. *Using this FUNCTION:* > > CREATE OR REPLACE FUNCTION same_test(did numeric) > RETURNS numeric AS $$ > BEGIN >   IF $1 IN >       (SELECT dealid from hygiene_112) >   THEN >     UPDATE hygiene_119 SET paid = 'SAME'; >   ELSE >       UPDATE hygiene_119 SET paid = '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. > > *Does the following query input the the dealids that result from the > SELECT statement into the parameter of the sames_test() FUNCTION?* > > 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); > > I doubt it does (my query runs a /long time)/ :-). I know I can utilize > python to push SELECT results into a array and then run a 'FOR d in > dealids' LOOP to feed the FUNCTION parameter but I'd like to learn how > to do that with nested SQL statements or FUNCTIONS. > > Thanks! > > > Hagen > -- Adrian Klaver adrian.klaver@aklaver.com