我曾经听说 CTE(WITH 语句)需要返回一些东西。否则可能会发生不执行的情况。
我有以下 CTE 简化结构:
WITH
selectA AS (
SELECT ...
),
insertA AS (
INSERT INTO ...
-- selecting something from selectA
RETURNING ...
),
insertB AS (
INSERT INTO ...
-- selecting something from selectA and insertA
-- HERE no RETURNING!
),
selectB AS (
SELECT ...
-- selecting something from selectA and insertA
),
insertForms AS (
INSERT INTO ...
-- selecting something from selectB
-- HERE no RETURNING!
),
insertFormData AS (
INSERT INTO ...
-- selecting something from selectB
-- HERE no RETURNING!
)
SELECT ...
-- selecting something from insertA and selectB
它有效,但我想知道我是否遇到了导致所有 CTE 部分都被执行的极端情况。 在 PostgreSQL 文档中,我没有找到 CTE 中是否需要返回部分的明确提示。
有人对此有好的想法吗? 非常感谢!
最佳答案
“但我想知道我是否遇到了任何极端情况”- 该行为已被完整记录 in the manual
If RETURNING is omitted, the statement is still executed, but it produces no output so it cannot be referenced as a table by the primary query
关于PostgreSQL:我需要在 CTE 中返回任何内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46968361/