如何在 PL/pgSQL 中 "PERFORM" CTE 查询?

How to "PERFORM" CTE queries in PL/pgSQL?

我试图在下面的代码示例中模拟我的问题。在下面的代码中,我在一个过程中执行 select * from test 。正如我们所知,我们必须为此使用 perform 关键字。这很好用:

perform * from test;

但是,如果我尝试将这个简单的查询重写为 CTE,我将无法正常工作。我收到语法错误。

with test_as_cte as(select * from test) perform * from test_as_cte;

这可能吗?正确的语法是什么?我尝试了几种替代方法并浏览了文档,但到目前为止没有任何成功。

(请注意,这只是解释我的问题的示例。我知道这些查询没有任何意义。)

create table test
(
    key int primary key  
);

create function test() returns trigger as
$$
begin
    raise notice 'hello there';
    -- this does work
    perform * from test;
    -- this doesn't work
    with test_as_cte as(select * from test) perform * from test_as_cte;
    return new;
end;
$$
language plpgsql;

create trigger test after insert on test for each row execute procedure test();

insert into test(key) select 1;

尝试:

perform (with test_as_cte as(select * from test) select * from test_as_cte);

我永远不会认为您可能需要 CTE 并忽略结果,但如果您需要,考虑执行 "select no return" 逻辑会导致上述语义。或 perform * from (CTE) 或类似的