1.光标变量:与光标类似,光标变量也是指向一个查询结果集的当前行。
2.光标:是一个PL/SQL结构,利用光标可以命名这些工作区,并通过光标访问工作区中的信息。
3.光标(静态游标)和光标变量(动态游标)定义不同,用法不同:
1)光标是一个静态的概念,光标变量是一个动态概念
2)光标变量不与一个特定的查询语句捆绑在一块,用户可以为任何一个查询语句打开一个光标变量。
3)光标变量类似于C语言中的指针,其内容是保存查询结果的内存地址(而非结果集本身)。
4)光标变量的类型是:ref cursor
5)一个显示光标总死代表同一个工作区,一个光标变量可以通过不同的赋值只想不同的工作区。
4.为什么使用光标变量:主要目的是利用它在PL/SQL,存储子程序以及各种Client程序之间传递查询结果集。
5.光标变量的定义:定义一个动态游标(光标变量)ref cursor类型:TYPE CurType is ref cursor;
注意:
a. 定义光标变量类型时,还可以规定返回类型必须表示数据库表中的一个记录型,如:TYPE EmpCurType is ref cursor return emp%ROWTYPE
b. 光标变量分为 强类型 和 弱类型;强类型指带有返回类型的ref cursor;弱类型指没有返回类型的ref cursor
6.使用光标变量:要用到3个操作
1)打开光标变量(Open ... for):open emp_cv for select * from emp;
功能:
a.将一个光标变量同一个查询语句相关联
b.执行查询语句
c.表示查询结果集
2)读取光标变量(Fetch):fetch语句从光标变量只想的结果集中每次读取一行数据,例如:
loop
fetch emp_cv into emp_rec;
exit when emp_cv%NOTFOUND;
....
end loop;
3)关闭光标变量(close): close emp_cv
完整示例:目前有错
create or replace procedure EMP_REF_Cursor is
TYPE LibCurTyp is ref cursor;
lib_ev LibCurTyp;
v_title titles.title%TYPE;
v_code titles.code%TYPE;
b_code books.bcode%TYPE;
p_code periodical.pcode%TYPE;
c_code cd.ccode%TYPE;
begin
--select title into v_title from temp;
select code,title into v_code,v_title from titles where title=v_title;
if v_code = 1 then
open lib_ev for select * from books where booktitle=v_title;
fetch lib_ev into b_code;
dbms_output.put_line('1....:'||b_code);
elsif v_code=2 then
open lib_ev for select * from periodical where ptitle=v_title;
fetch lib_ev into p_code;
elsif v_code=3 then
open lib_ev for select * from cd where ctitle=v_title;
fetch lib_ev into c_code;
end if;
end EMP_REF_Cursor;
TYPE LibCurTyp is ref cursor;
lib_ev LibCurTyp;
v_title titles.title%TYPE;
v_code titles.code%TYPE;
b_code books.bcode%TYPE;
p_code periodical.pcode%TYPE;
c_code cd.ccode%TYPE;
begin
--select title into v_title from temp;
select code,title into v_code,v_title from titles where title=v_title;
if v_code = 1 then
open lib_ev for select * from books where booktitle=v_title;
fetch lib_ev into b_code;
dbms_output.put_line('1....:'||b_code);
elsif v_code=2 then
open lib_ev for select * from periodical where ptitle=v_title;
fetch lib_ev into p_code;
elsif v_code=3 then
open lib_ev for select * from cd where ctitle=v_title;
fetch lib_ev into c_code;
end if;
end EMP_REF_Cursor;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29056818/viewspace-768451/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29056818/viewspace-768451/