psql函数(sp_some_function)返回2个游标:
create or replace function sp_some_function() RETURNS SETOF REFCURSOR
as
$BODY$
declare
c_one refcursor := 'one' ;
c_two refcursor := 'two' ;
begin
open c_one for
select *
from TABLE_1;
return next c_one ;
open c_two for
select *
from TABLE_2;
return next c_two ;
return;
end
$BODY$
LANGUAGE PLPGSQL
我想查看两个游标“包含”哪些数据。
为此,我编写了以下脚本:
DO $$
BEGIN
select sp_some_function();
FETCH ALL IN "one";
FETCH ALL IN "two";
END;
$$;
运行脚本会导致以下错误消息:
ERROR: "one" is not a known variable
我还尝试了以下方法:
DO $$
BEGIN
select sp_some_function();
FETCH ALL IN c_one;
FETCH ALL IN c_two;
END;
$$;
这导致出现以下错误消息:
ERROR: c_one is not a known variable
最后,我尝试了以下操作:
create or replace function sp_some_function() RETURNS SETOF REFCURSOR
as
$BODY$
declare
c_one refcursor := 'one' ;
c_two refcursor := 'two' ;
begin
open c_one for
select *
from TABLE_1;
FETCH ALL IN c_one;
return next c_one ;
open c_two for
select *
from TABLE_2;
FETCH ALL IN c_two;
return next c_two ;
return;
end
$BODY$
LANGUAGE PLPGSQL
这也不起作用。
...如何获取两个光标的内容?
答案 0 :(得分:1)
您的PL / pgSQL代码错误。
在PL / pgSQL中,如果没有SELECT
,则不能使用INTO
。
最适合您的是做这样的事情(未经测试):
DECLARE
c refcursor;
a_row record;
BEGIN
FOR c IN
SELECT sp_some_function()
LOOP
LOOP
FETCH c INTO a_row;
EXIT IF NOT FOUND;
/* do something with the result row */
END LOOP;
END LOOP;
END;
答案 1 :(得分:-1)
请按照以下方法获取输出
执行以下功能
BEGIN; -- begin Transaction
select sp_some_function();
在这里,您将获得refcusor值副本,然后从中获取数据。例如:
FETCH ALL IN "refcursor_value1"; -- Execute it seperately to see the result
FETCH ALL IN "refcursor_value2"; -- Execute it seperately to see the result
END; -- end Transaction