微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 带PERFORM CTE查询的Postgres plpgsql

我试着在下面的代码示例中模拟我的问题.在下面的代码中,我在一个过程中执行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并忽略结果,但如果您需要,考虑执行“选择不返回”逻辑会导致上述语义.或者从(CTE)或类似地执行*

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐