Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBH9t-0003UW-L4 for pgsql-sql@arkaria.postgresql.org; Wed, 25 Apr 2018 09:56:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fBH9s-0004Bt-GQ for pgsql-sql@arkaria.postgresql.org; Wed, 25 Apr 2018 09:56:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBH9s-0004Bj-9G for pgsql-sql@lists.postgresql.org; Wed, 25 Apr 2018 09:56:16 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fBH9p-0000Bh-Ar for pgsql-sql@lists.postgresql.org; Wed, 25 Apr 2018 09:56:15 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by ringways.co.uk with esmtp (Exim 4.89) (envelope-from ) id 1fBH9n-000Gt6-BS for pgsql-sql@lists.postgresql.org; Wed, 25 Apr 2018 10:56:12 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: "pgsql-sql@lists.postgresql.org" Subject: Re: error in function, works when typed Date: Wed, 25 Apr 2018 10:56:11 +0100 User-Agent: KMail/1.9.10 References: <201804251006.11072.gary.stainburn@ringways.co.uk> <8483951524648767@web10o.yandex.ru> In-Reply-To: <8483951524648767@web10o.yandex.ru> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <201804251056.11152.gary.stainburn@ringways.co.uk> X-KLMS-Rule-ID: 1 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Lua-Profiles: 124030 [Apr 25 2018] X-KLMS-AntiSpam-Version: 5.8.1.0 X-KLMS-AntiSpam-Envelope-From: gary.stainburn@ringways.co.uk X-KLMS-AntiSpam-Rate: 0 X-KLMS-AntiSpam-Status: not_detected X-KLMS-AntiSpam-Method: none X-KLMS-AntiSpam-Info: LuaCore: 128 128 d7aa645f57ce6a6ceb7dd78eb333137e5e6b654c, {msgid_created_by_recepient}, ringways.co.uk:7.1.1;www.ringways.co.uk:7.1.1;eddie.ringways.co.uk:7.1.1;127.0.0.199:7.1.2;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1, DmarcAF: none X-KLMS-AntiSpam-Interceptor-Info: scan successful X-KLMS-AntiPhishing: Clean, 2018/04/16 12:23:15 X-KLMS-AntiVirus: Kaspersky Security 8.0 for Linux Mail Server, version 8.0.1.721, bases: 2018/04/25 02:31:00 #8451410 X-KLMS-AntiVirus-Status: Clean, skipped X-Spam-Score: -50.7 (--------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie2.ringways.co.uk", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: Hi Samed, Thank you for getting back to me. Unfortunately, that wouldn't compile. Instead I've done it as a separate statement and it works. [...] Content analysis details: (-50.7 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.1 SCORE_RCPTS Adding score for each recipient 0.1 SCORE_RCPTS Adding score for each recipient -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 1.0 RING_SAFE No description available. List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Samed, Thank you for getting back to me. Unfortunately, that wouldn't compile. Instead I've done it as a separate statement and it works. Gary create or replace function service_cleardown(SRID integer, UID integer)=20 RETURNS integer as $$ DECLARE ROWCOUNT integer; BEGIN select count(sr_id) into ROWCOUNT from service_receptions where sr_id =3D= =20 SRID; IF NOT FOUND THEN raise exception 'Reception ID invalid'; END IF; select count(sj_id) into ROWCOUNT from service_jobs where sj_date < CURRENT_DATE and sj_sr_id =3D SRID and sj_state < 90; if ROWCOUNT =3D 0 THEN return 0; END IF; insert into service_jobs_log (sj_id, sj_u_id, sj_text) select sj_id, UID,'Job cleared down' from service_jobs where sj_date < CURRENT_DATE and sj_sr_id =3D SRID and sj_state < 90; update service_jobs set sj_state=3D90=20 where sj_date < CURRENT_DATE and sj_sr_id =3D SRID and sj_state < 90; RETURN ROWCOUNT; END $$ LANGUAGE plpgsql; On Wednesday 25 April 2018 10:32:47 Samed YILDIRIM wrote: > Hi Garry, > =C2=A0 > It is related with your update line in the function. Update line is > finishing with returning but it does not have any target for the output. > You can use cte to achieve this. Update line should be like following.=20 > with sr_update_cte as (update service_jobs set sj_state=3D90 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0where sj_date < CURRENT_DATE and sj_s= r_id =3D SRID and sj_state < 90 > =C2=A0=C2=A0=C2=A0=C2=A0returning 1) select count(*) into ROWCOUNT from s= r_update_cte; > =C2=A0 > Best regards. > Samed YILDIRIM > =C2=A0 > =C2=A0 > =C2=A0 > 25.04.2018, 12:06, "Gary Stainburn" : > I am writing a function to clear down old jobs. > > As you can see below, the commands work when I type them in, but when I t= ry > to use them in a function, the insert fails. > > Anyone got an idea why? The error suggests that the select does not have a > destination, but it feeds the insert. > > ---- > create or replace function service_cleardown(SRID integer, UID integer) > RETURNS integer as $$ > DECLARE > =C2=A0=C2=A0ROWCOUNT integer; > BEGIN > =C2=A0=C2=A0select count(sr_id) into ROWCOUNT from service_receptions whe= re sr_id =3D > SRID; > =C2=A0=C2=A0IF NOT FOUND THEN > =C2=A0=C2=A0=C2=A0=C2=A0raise exception 'Reception ID invalid'; > =C2=A0=C2=A0END IF; > =C2=A0=C2=A0insert into service_jobs_log (sj_id, sj_u_id, sj_text) > =C2=A0=C2=A0=C2=A0=C2=A0select sj_id, UID,'Job cleared down' from service= _jobs > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0where sj_date < CURRENT_DATE and sj_s= r_id =3D SRID and sj_state < 90; > =C2=A0=C2=A0update service_jobs set sj_state=3D90 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0where sj_date < CURRENT_DATE and sj_s= r_id =3D SRID and sj_state < 90 > =C2=A0=C2=A0=C2=A0=C2=A0returning ROWCOUNT; > =C2=A0=C2=A0RETURN ROWCOUNT; > END > $$ LANGUAGE plpgsql; > ---- > > goole=3D# insert into service_jobs_log (sj_id, sj_u_id, sj_text) > =C2=A0=C2=A0=C2=A0select sj_id, 25,'Job cleared down' from service_jobs > =C2=A0=C2=A0=C2=A0where sj_date < CURRENT_DATE and sj_sr_id =3D 10 and sj= _state < 90; > INSERT 0 0 > goole=3D# update service_jobs set sj_state=3D90 where sj_date < CURRENT_D= ATE > and sj_sr_id =3D 10 and sj_state < 90; > UPDATE 0 > goole=3D# select service_cleardown(10,25); > ERROR: query has no destination for result data > CONTEXT: PL/pgSQL function "service_cleardown" line 11 at SQL statement > goole=3D# > =C2=A0 =2D-=20 Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk=20