pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSyntax error for UPDATE ... RETURNING INTO STRICT
7+ messages / 4 participants
[nested] [flat]
* Syntax error for UPDATE ... RETURNING INTO STRICT
@ 2019-12-03 10:12 Alexander Farber <alexander.farber@gmail.com>
0 siblings, 2 replies; 7+ messages in thread
From: Alexander Farber @ 2019-12-03 10:12 UTC (permalink / raw)
To: pgsql-general
Good morning,
why does not PostgreSQL 10.11 please like the -
CREATE OR REPLACE FUNCTION words_toggle_puzzle(
in_mid bigint
) RETURNS table (
out_puzzle boolean
) AS
$func$
UPDATE words_moves
SET puzzle = NOT puzzle
WHERE mid = in_mid
RETURNING puzzle
INTO STRICT out_puzzle;
$func$ LANGUAGE sql;
and fails with -
ERROR: 42601: syntax error at or near "INTO"
LINE 11: INTO STRICT out_puzzle;
^
LOCATION: scanner_yyerror, scan.l:1128
Thank you
Alex
P.S: Here the table description, with mid being the PK:
words_ru=> \d words_moves
Table "public.words_moves"
Column | Type | Collation | Nullable
| Default
---------+--------------------------+-----------+----------+------------------------------------------
mid | bigint | | not null |
nextval('words_moves_mid_seq'::regclass)
action | text | | not null |
gid | integer | | not null |
uid | integer | | not null |
played | timestamp with time zone | | not null |
tiles | jsonb | | |
score | integer | | |
letters | text | | |
hand | text | | |
puzzle | boolean | | not null | false
Indexes:
"words_moves_pkey" PRIMARY KEY, btree (mid)
"words_moves_gid_played_idx" btree (gid, played DESC)
"words_moves_uid_action_played_idx" btree (uid, action, played)
"words_moves_uid_idx" btree (uid)
Check constraints:
"words_moves_score_check" CHECK (score >= 0)
Foreign-key constraints:
"words_moves_gid_fkey" FOREIGN KEY (gid) REFERENCES words_games(gid) ON
DELETE CASCADE
"words_moves_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON
DELETE CASCADE
Referenced by:
TABLE "words_scores" CONSTRAINT "words_scores_mid_fkey" FOREIGN KEY
(mid) REFERENCES words_moves(mid) ON DELETE CASCADE
^ permalink raw reply [nested|flat] 7+ messages in thread
* RE: Syntax error for UPDATE ... RETURNING INTO STRICT
@ 2019-12-03 10:49 Patrick FICHE <Patrick.Fiche@aqsacom.com>
parent: Alexander Farber <alexander.farber@gmail.com>
1 sibling, 1 reply; 7+ messages in thread
From: Patrick FICHE @ 2019-12-03 10:49 UTC (permalink / raw)
To: Alexander Farber <alexander.farber@gmail.com>; pgsql-general
Hi Alexander,
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;
Regards,
Patrick Fiche
Database Engineer, Aqsacom Sas.
c. 33 6 82 80 69 96
[01-03_AQSA_Main_Corporate_Logo_JPEG_White_Low.jpg]<http://www.aqsacom.com/;
From: Alexander Farber <alexander.farber@gmail.com>
Sent: Tuesday, December 3, 2019 11:12 AM
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Syntax error for UPDATE ... RETURNING INTO STRICT
Good morning,
why does not PostgreSQL 10.11 please like the -
CREATE OR REPLACE FUNCTION words_toggle_puzzle(
in_mid bigint
) RETURNS table (
out_puzzle boolean
) AS
$func$
UPDATE words_moves
SET puzzle = NOT puzzle
WHERE mid = in_mid
RETURNING puzzle
INTO STRICT out_puzzle;
$func$ LANGUAGE sql;
and fails with -
ERROR: 42601: syntax error at or near "INTO"
LINE 11: INTO STRICT out_puzzle;
^
LOCATION: scanner_yyerror, scan.l:1128
Thank you
Alex
P.S: Here the table description, with mid being the PK:
words_ru=> \d words_moves
Table "public.words_moves"
Column | Type | Collation | Nullable | Default
---------+--------------------------+-----------+----------+------------------------------------------
mid | bigint | | not null | nextval('words_moves_mid_seq'::regclass)
action | text | | not null |
gid | integer | | not null |
uid | integer | | not null |
played | timestamp with time zone | | not null |
tiles | jsonb | | |
score | integer | | |
letters | text | | |
hand | text | | |
puzzle | boolean | | not null | false
Indexes:
"words_moves_pkey" PRIMARY KEY, btree (mid)
"words_moves_gid_played_idx" btree (gid, played DESC)
"words_moves_uid_action_played_idx" btree (uid, action, played)
"words_moves_uid_idx" btree (uid)
Check constraints:
"words_moves_score_check" CHECK (score >= 0)
Foreign-key constraints:
"words_moves_gid_fkey" FOREIGN KEY (gid) REFERENCES words_games(gid) ON DELETE CASCADE
"words_moves_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE
Referenced by:
TABLE "words_scores" CONSTRAINT "words_scores_mid_fkey" FOREIGN KEY (mid) REFERENCES words_moves(mid) ON DELETE CASCADE
Attachments:
image001.png (image/png, 7.6K) download
view image
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Syntax error for UPDATE ... RETURNING INTO STRICT
@ 2019-12-03 11:37 Alexander Farber <alexander.farber@gmail.com>
parent: Patrick FICHE <Patrick.Fiche@aqsacom.com>
0 siblings, 1 reply; 7+ messages in thread
From: Alexander Farber @ 2019-12-03 11:37 UTC (permalink / raw)
To: Patrick FICHE <Patrick.Fiche@aqsacom.com>; +Cc: pgsql-general
Thank you Patrick -
On Tue, Dec 3, 2019 at 11:49 AM Patrick FICHE <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
(or otherwise, in very strange cases - the SQL would fail and my
java-servlet would throw SQLException)
Regards
Alex
Attachments:
image001.png (image/png, 7.6K) download
view image
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Syntax error for UPDATE ... RETURNING INTO STRICT
@ 2019-12-03 15:27 Tom Lane <tgl@sss.pgh.pa.us>
parent: Alexander Farber <alexander.farber@gmail.com>
1 sibling, 0 replies; 7+ messages in thread
From: Tom Lane @ 2019-12-03 15:27 UTC (permalink / raw)
To: Alexander Farber <alexander.farber@gmail.com>; +Cc: pgsql-general
Alexander Farber <alexander.farber@gmail.com> writes:
> why does not PostgreSQL 10.11 please like the -
I think you are confusing plpgsql syntax with sql syntax.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Syntax error for UPDATE ... RETURNING INTO STRICT
@ 2019-12-03 15:29 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Alexander Farber <alexander.farber@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Adrian Klaver @ 2019-12-03 15:29 UTC (permalink / raw)
To: Alexander Farber <alexander.farber@gmail.com>; Patrick FICHE <Patrick.Fiche@aqsacom.com>; +Cc: pgsql-general
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Syntax error for UPDATE ... RETURNING INTO STRICT
@ 2019-12-03 16:24 Alexander Farber <alexander.farber@gmail.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 1 reply; 7+ messages in thread
From: Alexander Farber @ 2019-12-03 16:24 UTC (permalink / raw)
To: ; +Cc: pgsql-general
Thanks for your replies!
Tom has hinted that STRICT is pl/pgSQL syntax and not SQL
Regards
Alex
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Syntax error for UPDATE ... RETURNING INTO STRICT
@ 2019-12-03 19:34 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Alexander Farber <alexander.farber@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Adrian Klaver @ 2019-12-03 19:34 UTC (permalink / raw)
To: Alexander Farber <alexander.farber@gmail.com>; +Cc: pgsql-general
On 12/3/19 8:24 AM, Alexander Farber wrote:
> Thanks for your replies!
>
> Tom has hinted that STRICT is pl/pgSQL syntax and not SQL
I finally read the full function and see you declared the LANGUAGE as
sql. Now things make sense:)
> Regards
> Alex
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2019-12-03 19:34 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-12-03 10:12 Syntax error for UPDATE ... RETURNING INTO STRICT Alexander Farber <alexander.farber@gmail.com>
2019-12-03 10:49 ` Patrick FICHE <Patrick.Fiche@aqsacom.com>
2019-12-03 11:37 ` Alexander Farber <alexander.farber@gmail.com>
2019-12-03 15:29 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-12-03 16:24 ` Alexander Farber <alexander.farber@gmail.com>
2019-12-03 19:34 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-12-03 15:27 ` Tom Lane <tgl@sss.pgh.pa.us>
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