暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第17天|openGauss定义游标

原创 Catch the rainbow 2021-12-22
393

1.创建游标,且使用select子句指定游标返回的行,分别使用FETCH抓取数据,MOVE重定位游标

create table test(a int,b varchar(10));
insert into test values(1,'zhangsan1');
insert into test values(2,'zhangsan2');
insert into test values(3,'zhangsan3');
insert into test values(4,'zhangsan4'),(5,'zhangsan5'),(6,'zhangsan6');


start transaction;
CURSOR cursor1 FOR SELECT * FROM test ORDER BY 1;
select * from pg_cursors;
FETCH FORWARD 3 FROM cursor1;
FETCH BACKWARD 1 FROM cursor1;
CLOSE cursor1;
select * from pg_cursors;
end;


START TRANSACTION;
CURSOR cursor1 FOR SELECT * FROM test ORDER BY 1;
MOVE FORWARD 3 FROM cursor1;
FETCH 4 FROM cursor1;
CLOSE cursor1;
end;



2.在系统视图pg_cursors中查看游标

select * from pg_cursors;

3.创建一个使用游标的存储过程

create or replace procedure test_cursor
as
aa integer;
bb varchar(10);
cursor test_cur is SELECT a,b FROM test order by 1;
begin
if not test_cur%isopen then
open test_cur;
end if;
loop
fetch test_cur into aa,bb;
RAISE INFO 'id num: %' ,aa;
exit when test_cur%notfound;
end loop;
if test_cur%isopen then
close test_cur;
end if;
end;
/
call test_cursor();
drop procedure test_cursor;


4.清理数据

drop table test;


最后修改时间:2021-12-31 11:56:25
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论