--游标
--游标是Oracle为用户开设的一个数据缓冲区,存放SQL语句的执行结果
--Oracle数据库中执行的每个SQL语句都有对应的单独的游标
--游标的分类:
--a)隐式游标:处理单行SELECT INTO 和DML语句
--b)显式游标:处理SELECT语句返回的多行数据
--显式游标的应用
一、定义游标:cursor cursor-name....(*_cursor)
二、打开游标:open cursor-name
三、提取游标:fetch cursor-name into ...
四、对游标指针指向的记录进行处理
五、继续处理,直到没有可处理的记录
六、关闭游标 close cursor-name
--显式游标的属性
%isopen %found %notfound %rowcount
--使用游标
declare
/*声明游标*/
cursor dept_cursor is select deptno,dname from dept;
v_deptno dept.deptno%type;
v_dname dept.dname%type;
begin
/*打开游标*/
open dept_cursor;
/*循环取值*/
loop
/*将当前行数据提取到变量中*/
fetch dept_cursor into v_deptno,v_dname;
exit when dept_cursor%notfound;
dbms_output.put_line('部门号:'||v_deptno||'部门名:'||v_dname);
end loop;
close dept_cursor;
end;
//小练习
SQL> declare
2 /*声明游标*/
3 cursor emp_cursor is select ENAME,SAL,deptno from emp;
4 v_ename emp.ename%type;
5 v_sal emp.sal%type;
6 v_deptno emp.deptno%type;
7 begin
8 /*打开游标*/
9 open emp_cursor;
10 /*循环取值*/
11 loop
12 /*将当前行数据提取到变量中*/
13 fetch emp_cursor into v_ename,v_sal,v_deptno;
14 exit when emp_cursor%notfound;
15 if v_deptno=10 then
16 dbms_output.put_line('NAME:'||v_ename||'SAL:'||v_sal);
17 end if;
18 end loop;
19 close emp_cursor;
20 end;
备份地址: 【Oracle笔记(四)】