I have a table t1
create table t1 (
id int primary key not null,
name varchar(64),
str2 varchar(64)
);
I want to get the row ID and update a field str2
in this row.
I tried this:
WITH inserted AS (
insert into t1(id, name) values (38, 'www') returning *
) UPDATE t1 SET str2='aaa' WHERE id IN (SELECT id FROM inserted);
select * from t1;
I expected:
| id | name | str2 |
|----|------|--------|
| 38 | www | aaa |
But the actual result was:
| id | name | str2 |
|----|------|--------|
| 38 | www | (null) |
See this SQLFiddle for a reproduction scenario.
What am I doing wrong?
id
after the insert? You can achieve this withreturning id