Quantcast
Viewing all articles
Browse latest Browse all 207

Why can't rows inserted in a CTE be updated in the same statement?

In PostgreSQL 9.5, given a simple table created with:

create table tbl (    id serial primary key,    val integer);

I run SQL to INSERT a value, then UPDATE it in the same statement:

WITH newval AS (    INSERT INTO tbl(val) VALUES (1) RETURNING id) UPDATE tbl SET val=2 FROM newval WHERE tbl.id=newval.id;

The result is that the UPDATE is ignored:

testdb=> select * from tbl;┌────┬─────┐│ id │ val │├────┼─────┤│  1 │   1 │└────┴─────┘

Why is this? Is this limitation part of the SQL standard (i.e. present in other databases), or something specific to PostgreSQL that might be fixed in future? The WITH queries documentation says multiple UPDATEs are not supported, but does not mention INSERTs and UPDATEs.


Viewing all articles
Browse latest Browse all 207

Trending Articles