P
P
potapuff2011-09-12 15:07:16
PostgreSQL
potapuff, 2011-09-12 15:07:16

PostgreSQL using Update in Select;

PostgreSQL since version 8 supports the RETURNING keyword in UPDATE queries.

Why can't a SELECT be made from the results returned by an UPDATE?

Example:
Preparation: Check: WTF!!!

CREATE TABLE tbl( KEY int, val int);
Insert into tbl values (1,1);


UPDATE tbl SET val = 1
WHERE KEY = any('{0,1,2,3,4,5}'::int[])
returning KEY;



SELECT * from (
UPDATE tbl SET val = 1
WHERE KEY = any('{0,1,2,3,4,5}'::int[])
returning (KEY)) as foo;

ERROR: syntax error at or near "SET"
СТРОКА 2: UPDATE tbl SET val = 1

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
potapuff, 2011-09-12
@potapuff

thanks for the help. The answer was found on Stackoverflow - it's just a PG bug.
stackoverflow.com/questions/7191902/cannot-select-from-update-returning-clause-in-postgres

A
abarmot, 2011-09-12
@abarmot

If you just need to get data, read the result of the update like a normal select.

A
alexius2, 2011-09-13
@alexius2

Yesterday a new version was released - 9.1, it should now work. At least it's stated like this:

* Data modification (INSERT/UPDATE/DELETE) is now allowed in the WITH clause.

E
el777, 2011-09-12
@el777

... RETURNING*;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question