【发布时间】:2019-08-08 11:19:20
【问题描述】:
我遇到了一个包含多个更新语句的 CTE 表达式的奇怪问题。
我可以使用以下 SQL 重现:-
DROP TABLE IF EXISTS foo;
DROP TABLE IF EXISTS baa;
CREATE TABLE foo(id BIGSERIAL, attributes JSONB);
CREATE TABLE baa(id BIGSERIAL, attributes JSON );
INSERT INTO foo(attributes) SELECT jsonb_build_object('foo', 'baa');
WITH STEP_ONE AS (
UPDATE foo
SET attributes = attributes ||jsonb_build_object('foo2', 'baa2')
WHERE id = 1
RETURNING attributes->>'foo' AS foo_att,id
), STEP_TWO AS (
INSERT INTO baa(attributes)
SELECT json_build_object('foo', id)
FROM STEP_ONE
RETURNING id as baa_id
)
UPDATE foo
SET attributes = attributes ||jsonb_build_object('baa', baa_id)
FROM STEP_TWO
WHERE id = 1
这不会更新表 foo。但是用“SELECT * FROM STEP_TWO”替换最终更新显示记录存在。
这是 postgresql 中的错误吗?还是我在文档中遗漏了有关在 CTE 中多次更新单个表的内容?
Postgres 版本:
PostgreSQL 11.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
【问题讨论】:
标签: postgresql common-table-expression