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

openGauss每日一练第17天 | 游标的使用

原创 .key 2021-12-17
1470

第十七课

学习openGauss游标的使用

👉openGauss SQL学习参考资料
https://opengauss.org/zh/docs/2.1.0/docs/Developerguide/SQL%E8%AF%AD%E6%B3%95.html

学习目标

学习openGauss定义游标

  • 为了处理SQL语句,存储过程进程分配一段内存区域来保存上下文联系,游标是指向上下文区域的句柄或指针。借助游标,存储过程可以控制上下文区域的变化。

数据库环境

是我个人自己安装部署的OpenGauss单机版

OpenGauss数据库版本:2.1.0

课程学习

连接数据库

su - omm source /srv/BigData/OpenGauss/db1_env gsql -d postgres -p 26000 -r


1.准备数据

create schema tpcds;
CREATE TABLE tpcds.reason
(
r_reason_sk integer,
r_reason_id character(16),
r_reason_desc character(100)
);
INSERT INTO tpcds.reason values(3,'AAAAAAAABAAAAAAA','reason 1'),
(10,'AAAAAAAABAAAAAAA','reason 2'),(4,'AAAAAAAABAAAAAAA','reason 3'),
(10,'AAAAAAAABAAAAAAA','reason 4'),(10,'AAAAAAAABAAAAAAA','reason 5'),
(20,'AAAAAAAACAAAAAAA','reason 6'),(30,'AAAAAAAACAAAAAAA','reason 7');

create table company(name varchar(100), loc varchar(100), no integer);
insert into company values ('macrosoft',    'usa',          001);
insert into company values ('oracle',       'usa',          002);
insert into company values ('backberry',    'canada',       003);


2. SELECT语句,用一个游标读取一个表

–开始一个事务

start transaction;

–建立一个名为cursor1的游标。

CURSOR cursor1 FOR SELECT * FROM tpcds.reason ORDER BY 1;

–在系统视图pg_cursors中查看可用游标

select * from pg_cursors;


–抓取头3行到游标cursor1里

FETCH FORWARD 3 FROM cursor1;

–从当前关联位置开始,抓取前面的1行

FETCH BACKWARD 1 FROM cursor1;

–关闭游标并提交事务

CLOSE cursor1;
select * from pg_cursors;
end;


3. VALUES子句,用一个游标读取VALUES子句中的内容

–建立一个名为cursor2的游标

start transaction;
CURSOR cursor2 FOR VALUES(1,2),(0,3) ORDER BY 1;
FETCH FORWARD 2 FROM cursor2;
CLOSE cursor2;
end;


4. WITH HOLD游标的使用

–声明该游标在创建它的事务结束后仍可继续使用

DECLARE cursor1 CURSOR WITH HOLD FOR SELECT * FROM tpcds.reason ORDER BY 1;

–抓取接下来的3行

FETCH FORWARD 3 FROM cursor1;
CLOSE cursor1;


5.移动游标

START TRANSACTION;
CURSOR cursor1 FOR SELECT * FROM tpcds.reason ORDER BY 1;

–忽略游标cursor1的前3行

MOVE FORWARD 3 FROM cursor1;

–抓取游标cursor1的前4行

FETCH 4 FROM cursor1;
CLOSE cursor1;
end;


6.存储过程中使用游标

create or replace procedure test_cursor_1
as
    company_name    varchar(100);
    company_loc varchar(100);
    company_no  integer;

    cursor c1_all is --cursor without args
        select name, loc, no from company order by 1, 2, 3;
begin
    if not c1_all%isopen then
        open c1_all;
    end if;
    loop
        fetch c1_all into company_name, company_loc, company_no;
		RAISE INFO 'company_name: %' ,company_name;
        exit when c1_all%notfound;
    end loop;
    if c1_all%isopen then
        close c1_all;
    end if;
end;
/
call test_cursor_1();
drop procedure test_cursor_1;



7.清理数据

drop schema tpcds cascade;
drop table company;


课程作业

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

create table chenyq(c_id integer,name varchar(10));
insert into chenyq values (001,'mysql'),(002,'oracle'),(003,'pgsql');
declare cursor_chenyq cursor with hold for select * from chenyq order by 1;
fetch forward 3 from cursor_chenyq;
close cursor_chenyq;
start transaction;
cursor cursor_chenyq for select * from chenyq order by 1;
move forward 2 from cursor_chenyq;


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

select * from pg_cursors;


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

create or replace procedure p_cursor_chenyq
as
id integer;
last_name varchar(10);
cursor cursor_chenyq is --cursor without args
select c_id,name from chenyq order by 1,2;
begin
if not cursor_chenyq%isopen then
open cursor_chenyq;
end if;
loop
fetch cursor_chenyq into id,last_name;
RAISE INFO 'id: %',id;
exit when cursor_chenyq%notfound;
end loop;
if cursor_chenyq%isopen then
close cursor_chenyq;
end if;
end;
/
call p_cursor_chenyq();


4.清理数据

drop procedure p_cursor_chenyq;drop table chenyq;


写在最后

今天的作业打卡结束!🎉

最后,宣传一下自己创建的社区的打卡活动:零基础 21 天速通 openGuass 打卡活动报名贴!

🏅 是同一个活动哦,但是可以额外获得本社区的福利奖品!还不来参与?

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

评论

目录
  • 第十七课