oracle动态游标

oracle动态游标  

无论显式还是隐式游标,获取记录集的查询定义在游标使用前必须确定。定义在整个生命周期内不可以修改,被叫作静态游标。
动态游标是指,在游标声明时,不指定其查询定义,而是在游标打开时进行定义。
一、强类型动态游标:
游标类型已经确定。
语法:
type 游标类型 is ref cursor
 return 记录类型
SQL> begin
declare
type employee_type is ref cursor
return employees%rowtype;
e_count number;
employee employees%rowtype;
cu_employee employee_type;
begin
select count(*) into e_count from employees where employee_position='项目经理';
if e_count=0 then
open cu_employee for select * from employees;
else
open cu_employee for select * from employees where employee_position='项目经理';
end if;
fetch cu_employee into employee;
while cu_employee%found loop
dbms_output.put_line(employee.employee_name || ':' || employee.employee_position);
fetch cu_employee into employee;
end loop;
end;
end;
 /
王晓:开发经理
钟小平:高级工程师
刘俊:高级工程师
王龙:工程师
钟文:工程师
张三:测试工程师
李四:测试工程师
王五:测试工程师
Jamaly:
Asha:
MLing:
ling:数据库工程师
二、弱类型动态游标:
没有指定返回类型。
语法:
type 游标类型 is ref cursor
如:在表employees 中,如果无法获得项目经理的信息,则希望继续查询表managers 表以尝试在该表中获得项目经理。表的结构及数据如下所示。
MANAGER_ID MANAGER_NAME         MANAGER_POSITION
---------- -------------------- --------------------
         1 张一民               财务经理
         2 周波                 从事经理
         3 刘永                 项目经理
         4 王雪秋               项目经理
如:
SQL> begin
declare
type custom_type is ref cursor;
c_count number;
employee employees%rowtype;
manager managers%rowtype;
cu_custom custom_type;
begin
select count(*) into c_count from employees where employee_position='项目经理';
if c_count=0 then
open cu_custom for select * from managers where manager_position='项目经理';
fetch cu_custom into manager;
while cu_custom%found loop
dbms_output.put_line(manager.manager_name || ':' || manager.manager_position);
fetch cu_custom into manager;
end loop;
else
open cu_custom for select * from employees where employee_position ='项目经理';
fetch cu_custom into employee;
while cu_custom%found loop
dbms_output.put_line(employee.employee_name || ':' || employee.employee_position);
fetch cu_custom into employee;
end loop;
end if;
end;
end;
/
刘永:项目经理
王雪秋:项目经理
PL/SQL procedure successfully completed
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值