如何在PL/pgSQL中执行CTE查询(&Q)? [英] How to "PERFORM" CTE queries in PL/pgSQL?

查看:23
本文介绍了如何在PL/pgSQL中执行CTE查询(&Q)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在下面的代码示例中模拟我的问题。在下面的代码中,我在一个过程中执行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而忽略结果,但如果您需要的话,将Perform视为"SELECT NO RETURN"逻辑会导致上述语义。或perform * from (CTE)或类似

这篇关于如何在PL/pgSQL中执行CTE查询(&Q)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆