PostgreSQL游标使用举例
来源:互联网 发布:2017网络洗脑神曲 编辑:程序博客网 时间:2024/06/05 18:04
1、下面的代码会创建一个top100cur()函数,该函数返回一个匿名游标
--drop function top100cur();create function top100cur() returns refcursor as $$declare abc refcursor;beginopen abc for select * from person limit 100;return abc;end$$language plpgsql;SELECT top100cur();--返回匿名游标--fetch all from abc;--匿名游标,所以此行错误
2、下面的代码会创建的函数会返回一个名字为abc的游标(能发现差别吧?是的,是否匿名,取决于open游标时,该游标变量是否已经绑定)
--drop function top100cur();create function top100cur() returns refcursor as $$declare abc cursor for select * from person limit 100;beginopen abc;return abc;end$$language plpgsql;SELECT top100cur();--返回名字为abc的游标fetch all from abc;--此行正确,执行时,记得把此行与select行以前拖黑,然后F5
3、返回由调用者命名的游标
--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;
4、返回一个名字叫$1的游标
--drop function top100cur(refcursor);create function top100cur(refcursor) returns refcursor as $$declare $1 cursor for select * from person limit 100;beginopen $1;--不open的话,返回的名字为"$1"游标不能使用!return $1;end$$language plpgsql;SELECT top100cur('abc');fetch all from "$1";
5、返回一个匿名的游标
--drop function top100cur(refcursor);create function top100cur(refcursor) returns refcursor as $$declare $1 refcursor;beginopen $1 for select * from person limit 100;return $1;end$$language plpgsql;SELECT top100cur('abc');--fetch all from "$1";--匿名游标,所以此行错误
6、返回由调用者指定的游标(知道跟4和5的不同吧?4、5中,declare声明的游标变量名称太特殊了,把匿名参数的名字隐藏了……)
--drop function top100cur(refcursor);create function top100cur(refcursor) returns refcursor as $$declare abcdef refcursor;beginopen $1 for select * from person limit 100;return $1;end$$language plpgsql;SELECT top100cur('abc');fetch all from "abc";
--drop function top100cur(refcursor);create function top100cur(refcursor) returns refcursor as $$declare abcdef refcursor;begin--open defg for select * from person limit 100;--错误: "defg" 不是一个已知变量--return defg;-----------LINE 5: open defg for select * from person limit 100;end$$language plpgsql;
总结:
1、declare的变量,会导致函数参数被隐藏(如,declare $1后,则第一个匿名参数就被隐藏了);
2、declare只是声明一个游标,不会open游标,而没有open的游标是不能用的哦~
3、declare之后再open游标时,如果这个游标是个未绑定的(declare时没有cursor for XXX),那么open后得到的是一个匿名游标;
4、open操作的游标变量,要么是declare的,要么是当做参数传入的字符串;除此之外,报错!
关于PostgreSQL中对游标的描述,请参见:
《PostgreSQL 8.1 中文文档(PostgreSQL 中国 制作)》的36.8章
- PostgreSQL游标使用举例
- PostgreSQL游标使用举例
- PostgreSQL游标使用举例
- Postgresql 游标使用
- postgresql游标的简单使用
- postgresql对于各种游标的使用示例
- postgresql数据库的游标使用例子说明
- 游标代码举例
- 游标代码举例
- PostgreSQL 中存储过程中使用多游标(refcursor)
- Oracle游标创建和使用的语法和举例(显示和隐式游标)
- PostgreSQL 游标处理
- postgresql 游标简单例子
- SQL游标语法及举例
- SQL游标语法及举例
- SQL 游标语法与举例
- SQL游标语法及举例
- ORACLE动态游标实战举例
- 图的支配集问题
- UVa530 - Binomial Showdown
- UVa369 - Combinations
- Android Service学习笔记
- UVa10006 - Carmichael Numbers
- PostgreSQL游标使用举例
- UVa10007 - Count the Trees
- 让phpmailer支持中文名称的附件和邮件标题中文乱码(转)
- 双向链表
- 让sourceinsight支持查看makefile、kconfig以及.s代码方法
- UVa10303 - How Many Trees?
- C语言32个关键字(转)
- JavaScript中的Error对象及其异常的用法
- 第一章 快速入门