PostgreSQL游标示例(创建游标,并在函数中遍历之)

PostgreSQL游标示例(创建游标,并在函数中遍历之)
--drop function top100cur(refcursor);create function top100cur(refcursor) returns refcursor as $$beginopen $1 for select * from person limit 100;return $1;end$$language plpgsql;----------测试游标----------- SELECT top100cur('abc');-- fetch all from abc;-- drop function from2cur(refcursor,int,int);--这是一个返回游标中在一定范围内记录的函数--create function from2cur(refcursor,int,int)returns setof text as $$declare--声明一些下标变量pnam text;pno text;index int;lower int;upper int;beginindex:=1;lower:=$2;upper:=$3;fetch $1 into pnam,pno;--必须先fetch一条,否则found为falsewhile found loop--只在[lower,upper]区间的记录才返回--if lower<=index and upper>=index thenreturn next pnam||pno;end if;fetch $1 into pnam,pno;index:=index+1;--超过upper后,函数返回--if index>upper then return;end if;end loop;end$$language plpgsql;select top100cur('abc');--创建一个名字为abc的游标-- fetch all in abc;--测试游标select * from from2cur('abc',2,5);

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部