From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Alexander Farber <alexander.farber@gmail.com>
To: Patrick FICHE <Patrick.Fiche@aqsacom.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Syntax error for UPDATE ... RETURNING INTO STRICT
Date: Tue, 3 Dec 2019 07:29:10 -0800
Message-ID: <15a043b0-142d-7730-9ed1-2f2a37b876af@aklaver.com> (raw)
In-Reply-To: <CAADeyWhikeMyuowX9X2BjRddruD+q0SkviMC-H7KFdbyX3AZGw@mail.gmail.com>
References: <CAADeyWioUgxc3j884Ko7jhi3Bf5GB-YiYbpjXCX6_ufojZo1LQ@mail.gmail.com>
<DB6PR0501MB2359D8D7331F05021ECB257EEF420@DB6PR0501MB2359.eurprd05.prod.outlook.com>
<CAADeyWhikeMyuowX9X2BjRddruD+q0SkviMC-H7KFdbyX3AZGw@mail.gmail.com>
On 12/3/19 3:37 AM, Alexander Farber wrote:
> Thank you Patrick -
>
> On Tue, Dec 3, 2019 at 11:49 AM Patrick FICHE <Patrick.Fiche@aqsacom.com
> <mailto:Patrick.Fiche@aqsacom.com>> wrote:
>
>
> It seems that STRICT is the issue.____
>
> But why does your function return a table of boolean in this case ?____
>
> As it only updates one record, it would probably be easier to return
> a boolean only.____
>
> CREATE OR REPLACE FUNCTION words_toggle_puzzle(
> in_mid bigint
> ) RETURNS boolean
> AS
> $func$
> UPDATE words_moves
> SET puzzle = NOT puzzle
> WHERE mid = in_mid
> RETURNING puzzle;
> $func$ LANGUAGE sql;
>
>
> your suggestion works well, thank you.
>
> I wanted to use strict, because the mid is a PK - so there should always
> be an exactly one record that has been updated
Which you will get without STRICT:
https://www.postgresql.org/docs/11/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW
"For INSERT/UPDATE/DELETE with RETURNING, PL/pgSQL reports an error for
more than one returned row, even when STRICT is not specified. This is
because there is no option such as ORDER BY with which to determine
which affected row should be returned."
Though I still not sure what was wrong with your initial attempt?:
ERROR: 42601: syntax error at or near "INTO"
LINE 11: INTO STRICT out_puzzle;
^
LOCATION: scanner_yyerror, scan.l:1128
From the error it looks like a hidden space issue or something.
>
> (or otherwise, in very strange cases - the SQL would fail and my
> java-servlet would throw SQLException)
>
> Regards
> Alex
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, alexander.farber@gmail.com, Patrick.Fiche@aqsacom.com
Subject: Re: Syntax error for UPDATE ... RETURNING INTO STRICT
In-Reply-To: <15a043b0-142d-7730-9ed1-2f2a37b876af@aklaver.com>
References: <CAADeyWioUgxc3j884Ko7jhi3Bf5GB-YiYbpjXCX6_ufojZo1LQ@mail.gmail.com> <DB6PR0501MB2359D8D7331F05021ECB257EEF420@DB6PR0501MB2359.eurprd05.prod.outlook.com> <CAADeyWhikeMyuowX9X2BjRddruD+q0SkviMC-H7KFdbyX3AZGw@mail.gmail.com> <15a043b0-142d-7730-9ed1-2f2a37b876af@aklaver.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