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

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

原创 陶振兴 2021-12-17
303

作业:

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

omm=# create table student (id int,name char(10),age char(2));
CREATE TABLE
omm=# insert into student values (1,'titi','12'),(2,'pipi','15'),(3,'kiki','17'),(4,'lolo','18'),(5,'bibi','19');
INSERT 0 5
omm=# select * from student;
id | name | age
----+------------+-----
1 | titi | 12
2 | pipi | 15
3 | kiki | 17
4 | lolo | 18
5 | bibi | 19
(5 rows)

omm=# start transaction;
omm=# START TRANSACTION

omm=# cursor cursor1 for select * from student order by 1;
DECLARE CURSOR

omm=# select * from pg_cursors;
name | statement | is_holdable | is_binary | is_scrollable | creation_t
ime
---------+------------------------------------------------------+-------------+-----------+---------------+-------------------
------------
cursor1 | cursor cursor1 for select * from student order by 1; | f | f | t | 2021-12-17 14:45:3
0.680166+08
(1 row)

omm=# fetch forward 3 from cursor1;
id | name | age
----+------------+-----
1 | titi | 12
2 | pipi | 15
3 | kiki | 17
(3 rows)

omm=# FETCH BACKWARD 1 FROM cursor1;
id | name | age
----+------------+-----
2 | pipi | 15
(1 row)

omm=# CLOSE cursor1;
CLOSE CURSOR
omm=# select * from pg_cursors;
name | statement | is_holdable | is_binary | is_scrollable | creation_time
------+-----------+-------------+-----------+---------------+---------------
(0 rows)

omm=# end;
omm=# COMMIT


omm=# start transaction;
START TRANSACTION
omm=# cursor cursor1 for select * from student order by 1;
DECLARE CURSOR
omm=# select * from student order by 1;
id | name | age
----+------------+-----
1 | titi | 12
2 | pipi | 15
3 | kiki | 17
4 | lolo | 18
5 | bibi | 19
(5 rows)

omm=# move forward 2 from cursor1;
MOVE 2
omm=# fetch 3 from cursor1;
id | name | age
----+------------+-----
3 | kiki | 17
4 | lolo | 18
5 | bibi | 19
(3 rows)

omm=# close cursor1;
CLOSE CURSOR
omm=# end;
COMMIT

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

omm=# select * from pg_cursors;
name | statement | is_holdable | is_binary | is_scrollable | creation_t

omm=# start transaction;
omm=# START TRANSACTION

omm=# cursor cursor1 for select * from student order by 1;
DECLARE CURSOR

omm=# select * from pg_cursors;
name | statement | is_holdable | is_binary | is_scrollable | creation_t
ime 
---------+------------------------------------------------------+-------------+-----------+---------------+-------------------
------------
cursor1 | cursor cursor1 for select * from student order by 1; | f | f | t | 2021-12-17 14:45:3
0.680166+08
(1 row)

omm=# end;
COMMIT

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

omm=# create or replace procedure test_cursor_1
omm-# as
omm$# st_id integer;
omm$# st_name char(10);
st_age char(2);
omm$#
omm$# omm$# cursor c1_all is --cursor without args
omm$# select id ,name ,age from student order by 1, 2, 3;
omm$# begin
omm$# if not c1_all%isopen then
omm$# open c1_all;
omm$# end if;
omm$# loop
omm$# fetch c1_all into st_id, st_name, st_age;
omm$# RAISE INFO 'st_id: %' ,st_id;
omm$# exit when c1_all%notfound;
omm$# end loop;
omm$# if c1_all%isopen then
omm$# close c1_all;
omm$# end if;
omm$# end;
omm$# /
CREATE PROCEDURE
omm=# call test_cursor_1();
INFO: st_id: 1
INFO: st_id: 2
INFO: st_id: 3
INFO: st_id: 4
INFO: st_id: 5
INFO: st_id: 5
test_cursor_1
---------------
(1 row)

omm=# drop procedure test_cursor_1;
DROP PROCEDURE

4.清理数据

omm=# drop table student;
omm=# DROP TABLE

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

评论