Samdy_Chan 发表于 2012-03-20 14:34

请教关于游标变量和游标表达式的问题

大家好:
      以下PL/SQL程序代码,使用到了指针游标变量:empcur 和 游标表达式:cursor(select ename,sal from emp where deptno=a.deptno)
请问为何指针游标变量empcur在代码中没有使用到open empcur for 指定打开的select结果集语句,而且可以将别的游标数据(dept_cursor)直接into到该游标(empcur):fetch dept_cursor into v_dname,empcur;

游标变量不是要在open语句打开游标时,指定查询结果集才行的吗?怎么在这里可以直接保存dept_cursor游标的数据的?
cursor(select ename,sal from emp where deptno=a.deptno)这个游标表达式和游标变量empcur有什么关系呢?为什么游标empcur 可以保存游标表达式cursor(select ename,sal from emp where deptno=a.deptno)中的ename,sal列的数据: fetch empcur into v_ename,v_sal ??
是不是游标变量empcur 被隐含定义为cursor(select ename,sal from emp where deptno=a.deptno)这个游标变量的别名,所以可以直接引用empcur这个游标变量,而不用先open empcur for select ....了?
由于书上没有说明到这些,还望各位高手能详细指点,谢谢!
declare
type refcursor is ref cursor;
cursor dept_cursor(no number) is
    select a.dname,cursor(select ename,sal from emp where deptno=a.deptno)
    from dept a where a.deptno=no;
empcur refcursor;
v_dname dept.dname%type;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open dept_cursor(&no);
loop
    fetch dept_cursor into v_dname,empcur;
    exit when dept_cursor%notfound;
    dbms_output.put_line('部门名:'||v_dname);
    loop
      fetch empcur into v_ename,v_sal;
      exit when empcur%notfound;
      dbms_output.put_line('雇员名:'||v_ename||',工资:'||v_sal);
    end loop;
end loop;
close dept_cursor;
end;

lxg9165 发表于 2012-03-23 17:12

进来学习的~~~~~~~~~~~
















Signature------------------------------------------------------
We hope to get a long, Steady Business with all Sorts of buyers all over the World.
Christian Louboutin Daffodile
Christian Louboutin Flats
Christian Louboutin Pumps
Christian Louboutin Sandals


页: [1]
查看完整版本: 请教关于游标变量和游标表达式的问题