CREATE OR REPLACE FUNCTION cursor_demo()
RETURNS refcursor AS
$BODY$
declare
unbound_refcursor refcursor;
v_id int;
v_step_desc varchar(1000);
begin
open unbound_refcursor for execute 'select id,step_desc from t_runtime_step_log';
loop
fetch unbound_refcursor into v_id,v_step_desc;
if found then
raise notice '%-%',v_id,v_step_desc;
else
exit;
end if;
end loop;
close unbound_refcursor;
raise notice 'the end of msg...';
return unbound_refcursor;
exception when others then
raise exception 'error--(%)',sqlerrm;
end;
$BODY$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION cursor_demo1(refcursor)
RETURNS refcursor AS
$$
begin
open $1 for select * from t_runtime_step_log;
return $1;
exception when others then
raise exception 'sql exception--%',sqlerrm;
end;
$$
LANGUAGE plpgsql;
begin;
select cursor_demo1('a');
fetch all in a;
--commit;
CREATE OR REPLACE FUNCTION cursor_demo2()
postgresql对于各种游标的使用示例
最新推荐文章于 2024-07-08 08:49:34 发布
本文通过四个示例详细介绍了 PostgreSQL 中不同类型的游标用法,包括未绑定游标、带参数的游标以及在函数中操作游标的方法,展示了如何在PL/pgSQL中打开、读取和关闭游标。
摘要由CSDN通过智能技术生成