Oracle数据库游标

9 篇文章 0 订阅

pl/sql的变量:
1.标量类型:
1>普通类型
2>%type
3>%rowtype
2.复合类型:
1>记录
2>表
3>游标:
复合类型变量一次可以存储多个值,因为记录类型可以持有多个
成员进而一次可以存储多个值,表类型本质是数组所以也一次可以
存储多个值;但是记录类型和表类型一次存储的多个值只能是查询
到的一行多列的多个值,不能存储查询到的多行多列的值;那么存储
查询到的多行多列的值就得使用游标。

因为游标的本质是集合,存储了查询到的多行多列的虚拟表的集合,
而虚拟表的行就是游标的元素。

游标的分类:
1.REF游标:是被oracle数据库操作的游标,不由我们操作。
2.静态游标:
1>隐式游标:也是被oracle操作的游标,也不由我们操作。
2>显示游标:通常所说的游标

游标的操作流程:
1.声明游标:
cursor 游标名称 is select语句;

创建游标,并给游标绑定指定的select语句所查询的虚拟表。
将select语句所查询到的虚拟表封装到了游标中。

2.打开游标:
open 游标;
使用游标之前,必须先打开游标,否则无法操作游标(开启资源)。

3.提取游标数据:
使用关键字fetch来提取游标数据,就是每fetch一次就获取到
游标所封装的虚拟表的一行数据。
fetch的本质是在操作指针移动,即每fetch一次指针就向虚拟表的
下一行移动。

4.关闭游标:
close 游标;
关闭资源

游标常见的4大属性:
1.%found:判断fetch游标的当前行的数据是否存在,存在true,不存在false
2.%nofound:判断fetch游标的当前行的数据是否不存在,不存在true,存在false
3.%isopen:判断游标是否开启,开启返回true,关闭返回false
4.%rowcount:获取游标元素个数(游标所封装的虚拟表的行数)
*/
–录入部门编号,查询该部门所有员工的姓名 工资
declare
–1.声明游标并给其绑定指定的select语句所生成的虚拟表
cursor emp_cursor is select ename,sal from emp where deptno=&no;
v_name emp.ename%type;—接收提取的每行的ename列的值
v_sal emp.sal%type;—接收每次提取的每行的sal列的值
begin
–2.开启游标
open emp_cursor;
–3.提取游标数据
–将提取的每行的ename和sal列的值寄存给变量v_name和v_sal
fetch emp_cursor into v_name,v_sal;
dbms_output.put_line(v_name||’ ‘||v_sal);

fetch emp_cursor into v_name,v_sal;
dbms_output.put_line(v_name||’ ‘||v_sal);

fetch emp_cursor into v_name,v_sal;
dbms_output.put_line(v_name||’ ‘||v_sal);

fetch emp_cursor into v_name,v_sal;
dbms_output.put_line(v_name||’ ‘||v_sal);

fetch emp_cursor into v_name,v_sal;
dbms_output.put_line(v_name||’ ‘||v_sal);

fetch emp_cursor into v_name,v_sal;
dbms_output.put_line(v_name||’ ‘||v_sal);

–4.关闭游标
close emp_cursor;
end;

–loop循环遍历游标
declare
–1.声明游标并给其绑定指定的select语句所生成的虚拟表
cursor emp_cursor is select ename,sal from emp where deptno=&no;
v_name emp.ename%type;—接收提取的每行的ename列的值
v_sal emp.sal%type;—接收每次提取的每行的sal列的值
begin
–2.开启游标
open emp_cursor;
/*
3.循环遍历提取游标数据
*/
loop
fetch emp_cursor into v_name,v_sal;
–当fetch游标的当前行数据不存在时,结束循环
if emp_cursor%notfound then
exit;
end if;
dbms_output.put_line(v_name||’ ‘||v_sal);
end loop;
–4.关闭游标
close emp_cursor;
end;

–使用while循环遍历游标

declare
–1.声明游标并给其绑定指定的select语句所生成的虚拟表
cursor emp_cursor is select ename,sal from emp where deptno=&no;
v_name emp.ename%type;—接收提取的每行的ename列的值
v_sal emp.sal%type;—接收每次提取的每行的sal列的值
begin
–2.开启游标
open emp_cursor;
/*
3.使用while循环遍历游标:
*/
–先fetch一行数据
fetch emp_cursor into v_name,v_sal;
–fetch的当前行数据存在时则进行循环体
while emp_cursor%found loop
dbms_output.put_line(v_name||’ ‘||v_sal);
–再fetch
fetch emp_cursor into v_name,v_sal;
end loop;
–4.关闭游标
close emp_cursor;
end;

–for循环遍历游标
/*
是遍历游标最简洁的方式:
1.不需要我们手动开启和关闭游标,会自动开启和关闭游标
2.遍历游标的方式类似于java的foreahc循环:
for 循环变量 in 游标 loop
每循环一次,循环变量就代表游标的一行
end loop;
*/
declare
–1.声明游标
cursor emp_cursor is select ename,sal from emp where deptno=&no;
v_name emp.ename%type;
v_sal emp.sal%type;
begin
for e in emp_cursor loop
v_name:=e.ename;
v_sal:=e.sal;
dbms_output.put_line(v_name||’ ‘||v_sal);
end loop;
end;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值