这是我的现实问题的一个最小示例:
create table t(id serial primary key, rnd double precision);
当然,您可以使用returning
子句返回插入的列:
with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID | RND |
|----|----------------|
| 9 | 0.203221440315 |
*/
您还可以返回文字:
with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *, 1.0 dummy;
/*
| ID | RND | DUMMY |
|----|----------------|-------|
| 11 | 0.594980469905 | 1 |
*/
但您无法返回源列:
with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *, w.rnd;
/*
ERROR: missing FROM-clause entry for table "w": with w as (insert into t(rnd) values(random()) returning *) insert into t(rnd) select random() from w returning *, w.rnd
*/
有什么办法可以使我w.rnd
摆脱最终returning
条款?
db <> 在这里拨弄
在MS SQL Server中,仅MERGE语句允许返回其他列。也许这也适用于postgres。
—
塞巴斯蒂安·梅恩