静态游标:与一个sql语句关联,编译块时候,语句已知
动态游标:运行时候与不同的sql关联
游标变量:
类似于pl/sql变量,可以在运行时接纳不同的值.
定义游标变量的完整语法:
1.具有返回类型的,称为有约束的游标或者强游标
type type_name is ref cursor return return_type;
2.没有返回类型的,称为无约束的游标或者弱游标
type type_name is ref cursor ;
3.返回值为记录类型
弱游标:
--------------------------------------------------------------------------------
declare
type ref_cur is ref cursor;
v_cur ref_cur;
ab varchar2(10);
vals varchar2(20);
begin
ab:='&input_choice';
if upper(ab)='A' then
open v_cur for select ename
from emp
where empno=7369;
fetch v_cur into vals;
dbms_output.put_line('姓名:'||vals);
close v_cur;
else
open v_cur for select dname
from dept
where deptno=10;
fetch v_cur into vals;
dbms_output.put_line('部门名称:'||vals);
close v_cur;
end if;
end;
/
--------------------------------------------------------------------------
强游标:用记录集
declare
type recordtype is record(
r_deptno number(2),
r_dname varchar2(14),
r_loc varchar2(13)
);
type rt_cur is ref cursor return dept%rowtype;
vrt_cur rt_cur;
value_rt recordtype ;
begin
open vrt_cur for select deptno,dname,loc
from dept;
loop
fetch vrt_cur into value_rt ;
exit when vrt_cur%notfound;
dbms_output.put_line('编号 '||value_rt.r_deptno||' 部门 '||value_rt.r_dname );
end loop;
close vrt_cur ;
end;
----------------------------------------------------------------------------
强游标:用表
declare
type rt_cur is ref cursor return dept%rowtype;
vrt_cur rt_cur;
value_rt dept%rowtype;
begin
open vrt_cur for select deptno,dname,loc
from dept;
loop
fetch vrt_cur into value_rt ;
exit when vrt_cur%notfound;
dbms_output.put_line('编号 '||value_rt.deptno||' 部门 '||value_rt.dname );
end loop;
close vrt_cur ;
end;