简体   繁体   中英

how to return all the columns not just the updated columns when update in postgresql?

let's say i have this update statement

UPDATE person set age = 20,location = 'us' WHERE id = 1234

based from documentation of postgresql on UPDATE, it can only return updated fields. but I want to get all fields regardless if it's updated or not

RETURNING *

doesn't work

returning * will return all columns of the row that was updated.

Quote from the manual

If the UPDATE command contains a RETURNING clause, the result will be similar to that of a SELECT statement containing the columns and values defined in the RETURNING list

So the returning * works pretty much like select *

postgres=# create table foo (id int, c1 int, c2 int, c3 int, c4 int);
CREATE TABLE
postgres=# insert into foo values (1, 2, 3, 4, 5);
INSERT 0 1
postgres=# update foo set c4 = 42 where id = 1 returning *;
 id | c1 | c2 | c3 | c4
----+----+----+----+----
  1 |  2 |  3 |  4 | 42
(1 row)

UPDATE 1
postgres=#

Online example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM