游标"/>
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
发布评论