加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 带PERFORM CTE查询的Postgres plpgsql

发布时间:2020-12-13 16:00:56 所属栏目:百科 来源:网络整理
导读:我试着在下面的代码示例中模拟我的问题.在下面的代码中,我在一个过程中执行select * from test.我们知道,我们必须使用perform关键字.这非常有效: perform * from test; 但是,如果我尝试将该简单查询重写为CTE,我无法使其正常工作.我收到语法错误. with test
我试着在下面的代码示例中模拟我的问题.在下面的代码中,我在一个过程中执行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)或类似地执行*

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读