SQLite Forum

Use of RETURNING clause as a subquery
Login
Well, none of the above trials fared any better in PostgreSQL. (It moans, "ERROR:  WITH clause containing a data-modifying statement must be at the top level".) However, this screen-scrape shows what does succeed:<code>
postgres=# CREATE TABLE t0(
postgres(#    a INTEGER PRIMARY KEY,
postgres(#    b DATE DEFAULT CURRENT_TIMESTAMP,
postgres(#    c INTEGER
postgres(#  );
CREATE TABLE
postgres=# create table t0log (whenCreated DATE, what integer);
CREATE TABLE
postgres=# with stuffed as (INSERT INTO t0(a,b,c) VALUES(5,CURRENT_DATE,random()) returning b,c) insert into t0log(whenCreated,what) select b,c from stuffed;
INSERT 0 1
postgres=# select * from t0log;
 whencreated | what
-------------+------
 2021-02-03  |    1
(1 row)
</code>.

(The 1 came in the what column comes from PostgreSQL's random() returning a float between 0 and near 1, which is rounded going into t0.c, and integer.)

I will suggest a few RETURNING doc changes as I study it further.