我想知道如何在函数内使用动态查询。我尝试了很多方法,但是,当我尝试编译我的函数时,会显示一条消息 SQL 42601。
我使用的代码:
CREATE OR REPLACE FUNCTION prc_tst_bulk(sql text)
RETURNS TABLE (name text, rowcount integer) AS
$$
BEGIN
WITH v_tb_person AS (return query execute sql)
select name, count(*) from v_tb_person where nome like '%a%' group by name
union
select name, count(*) from v_tb_person where gender = 1 group by name;
END
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息:
ERROR: syntax error at or near "return"
LINE 5: WITH v_tb_person AS (return query execute sql)
Run Code Online (Sandbox Code Playgroud)
我尝试使用:
WITH v_tb_person AS (execute sql)
WITH v_tb_person …Run Code Online (Sandbox Code Playgroud) 在PostgreSQL中存在一些使用批量收集的声明的方法,就像在Oracle中一样?
Oracle中的示例:
create or replace procedure prc_tst_bulk_test is
type typ_person is table of tb_person%rowtype;
v_tb_person typ_person;
begin
select *
bulk collect into v_tb_person
from tb_person;
-- make a selection in v_tb_person, for instance
select name, count(*) from v_tb_person where age > 50
union
select name, count(*) from v_tb_person where gender = 1
end;
Run Code Online (Sandbox Code Playgroud)
谢谢