pl/sql游标

通过游标,我们可以取得返回结果集的任何一行记录,提高效率。

定义游标:

type 游标名 is ref cursor

变量名 游标名

打开游标:

open 游标变量 for select语句;

取出当前游标指向的行:

fetch 游标变量 into 其它变量;

判断游标是否指向最后记录:

游标变量%nofound

案例:编写一个pl/sql过程,要求输入部门号,并显示该部门所有员工和姓名和工资

create or replace procedure pro1(v_in_deptno in number)
is
--先定义一个游标变量类型
type emp_cursor is ref cursor;
--定义一个游标变量
v_emp_cursor emp_cursor;
--定义两个变量
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open v_emp_cursor for select ename,sal from emp where deptno=v_in_deptno;
--取出游标指向的数据
loop
fetch v_emp_cursor into v_ename,v_sal;
--游标到达最后时退出
exit when v_emp_cursor%notfound;
dbms_output.put_line('用户名 '||v_ename||' 工资 '||v_sal);
end loop;
--关闭游标
close v_emp_cursor;
end;

 

posted @ 2014-01-10 11:01  我爱java2013  阅读(208)  评论(0编辑  收藏  举报