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
What Is EXCLUDED in PostgreSQL. EXCLUDED is the name the DBMS gives to a special table where we have all the rows proposed for INSERTION present. These rows may be inserted to this table as soon as the INSERT operation runs. This is mostly preceded by the ON CONFLICT DO UPDATE clause, specifically targeting this table.
SELECT statements An SQL SELECT statement retrieves records from a database table according to clauses (for example, FROM and WHERE ) that specify criteria. The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value';
In the CREATE TABLE statement, you specify a comma-separated list of column definitions. Each column definition is composed of a column name, column's data type, a default value, and one or more column constraints.
In SQL Server, you can execute the sp_columns command to list all the columns in a table.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With