6
 WITH upt as (
 UPDATE backend."orders" SET "statusId" = 5
 WHERE "userId" IN (177962,88265) and "statusId" IN (0,1,2,3,4) RETURNING *
)
 INSERT INTO __test_result(orderid) VALUES ((SELECT orderid FROM upt))

Need to update and log data,getting this error

ERROR: column "orderid" does not exist Hint: There is a column named 
"orderid" in table "__test_result", but it cannot be referenced from this part of the query.

How can I insert in table for all "upt" rows?It must look

"upt.orderid","jsonb for that orderid"

for every order jsonb must be created from "upt" column with same orderid

Grigor
  • 499
  • 1
  • 9
  • 19

1 Answers1

5

If you want to use a select as the source for an insert (for multiple rows) do not use the values clause, use the select directly: insert into .. select ....

So in your case:

WITH upt as (
  UPDATE backend."orders" 
     SET "statusId" = 5
  WHERE "userId" IN (177962,88265) 
    and "statusId" IN (0,1,2,3,4) 
  RETURNING *
)
INSERT INTO __test_result(orderid) 
SELECT orderid 
FROM upt;