CREATE OR REPLACE FUNCTION cursor_demo()
RETURNS refcursor AS --返回一个游标
$BODY$
declare --定义变量及游标
unbound_refcursor refcursor; --游标
t_accid varchar; --变量
t_accid2 int; --变量
begin --函数开始
open unbound_refcursor for execute 'select name from cities_bak'; --打开游标 并注入要搜索的字段的记录
loop --开始循环
fetch unbound_refcursor into t_accid; --将游标指定的值赋值给变量
if found then --任意的逻辑
raise notice '%-',t_accid;
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; --规定语言
select cursor_demo(); --调用