包游标即在包头声明部分定义游标头,即游标的定义,把实现部分留给包体来完成,调用时可以通过包来调用,这样方便共享游标,实际实现功能类似视图。
-
示例
包头定义
create or replace package mypackage is
cursor cur_employee(deptno in employee.departmentno%type) return employee%rowtype; --游标的定义一定要包含return returnType
end;
包体定义
create or replace package body mypackage is
cursor cur_employee(deptno in employee.departmentno%type) return employee%rowtype
is select * from employee where departmentno=deptno;
end;
调用
declare
item employee%rowtype;
begin
if mypackage.cur_employee%isopen then --因为包游标作为共享游标,一定要用完关闭,用前判断是否已打开
close mypackage.cur_employee;
end if;
open mypackage.cur_employee('005');
loop
fetch mypackage.cur_employee into item;
exit when mypackage.cur_employee%notfound;
dbms_output.put_line(item.employeename);
end loop;
close mypackage.cur_employee; ---用完关闭
end;
执行结果
黄奋
王加红
章常勇
李红
标签:end,cur,游标,employee,使用,oracle,employee%,mypackage From: https://www.cnblogs.com/sundh1981/p/17433060.html