Skip to main content
edited tags
Source Link
Jack Douglas
  • 40.2k
  • 15
  • 104
  • 177

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);

of course you can return the inserted columns with a returning clause:

with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID |            RND |
|----|----------------|
|  9 | 0.203221440315 |
*/

you can also return a literal:

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 |
*/

but you can't return the source columns:

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
*/

Is there any way I can get w.rnd out of the final returning clause?

SQLFiddledb<>fiddle here

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);

of course you can return the inserted columns with a returning clause:

with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID |            RND |
|----|----------------|
|  9 | 0.203221440315 |
*/

you can also return a literal:

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 |
*/

but you can't return the source columns:

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
*/

Is there any way I can get w.rnd out of the final returning clause?

SQLFiddle

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);

of course you can return the inserted columns with a returning clause:

with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID |            RND |
|----|----------------|
|  9 | 0.203221440315 |
*/

you can also return a literal:

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 |
*/

but you can't return the source columns:

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
*/

Is there any way I can get w.rnd out of the final returning clause?

db<>fiddle here

Tweeted twitter.com/#!/StackDBAs/status/383973655128313856
added explaining comments
Source Link
Jack Douglas
  • 40.2k
  • 15
  • 104
  • 177

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);
 

of course you can return the inserted columns with a returning clause:

with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID |            RND |
|----|----------------|
|  9 | 0.203221440315 |
 
*/
 

you can also return a literal:

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 |
*/
 

but you can't return the source columns:

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
*/

Is there any way I can get w.rnd out of the final returning clause?

SQLFiddle

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);
 
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
*/

Is there any way I can get w.rnd out of the final returning clause?

SQLFiddle

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);

of course you can return the inserted columns with a returning clause:

with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID |            RND |
|----|----------------|
|  9 | 0.203221440315 |
*/

you can also return a literal:

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 |
*/

but you can't return the source columns:

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
*/

Is there any way I can get w.rnd out of the final returning clause?

SQLFiddle

oops
Source Link
Jack Douglas
  • 40.2k
  • 15
  • 104
  • 177

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);

with w as (insert into t default(rnd) values(random()) returning id*)
insert into t(rnd) defaultselect valuesrandom() from w returning id;*;
/*
| idID |            RND |
|----|----------------|
| 1 9 | 0.203221440315 |

*/

with w as (insert into t default(rnd) values(random()) returning id*)
insert into t(rnd) defaultselect valuesrandom() from w returning id*, 'a'1.0 dummy;
/*
| idID | dummy           RND | DUMMY |
|----+|----------------|-------|
| 311 | 0.594980469905 | a    1 |
*/

with w as (insert into t default(rnd) values(random()) returning id*)
insert into t(rnd) defaultselect valuesrandom() from w returning id*, w.id;rnd;
/*
ERROR:  missing FROM-clause entry for table "w"
LINE 2: with w as (insert into t(rnd) values(random()) returning *) insert into t(rnd) defaultselect valuesrandom() from w returning id*, w.id;rnd
*/

Is there any way I can get w.idrnd out of the final returning clause?

SQLFiddleSQLFiddle

Here's a minimal example of my real-world problem:

create table t(id serial primary key);

with w as (insert into t default values returning id)
insert into t default values returning id;
/*
 id
----
 1
*/

with w as (insert into t default values returning id)
insert into t default values returning id, 'a' dummy;
/*
 id | dummy
----+-------
 3  | a
*/

with w as (insert into t default values returning id)
insert into t default values returning id, w.id;
/*
ERROR:  missing FROM-clause entry for table "w"
LINE 2:     insert into t default values returning id, w.id;
*/

Is there any way I can get w.id out of the final returning clause?

SQLFiddle

Here's a minimal example of my real-world problem:

create table t(id serial primary key, rnd double precision);

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
*/

Is there any way I can get w.rnd out of the final returning clause?

SQLFiddle

Source Link
Jack Douglas
  • 40.2k
  • 15
  • 104
  • 177
Loading