日期:2014-05-17  浏览次数:20676 次

新手学习游标时出现的错误,请求大家帮助。
SQL code

set serveroutput on;
declare 
    type stuRecordFor is record
    (sNum MyStudent.stu_num%type,
     sName MyStudent.stu_name%type
    );
    numName stuRecordFor;
    cursor curFor(varSex varchar2) is select stu_num, stu_name from MyStudent where stu_sex = varSex;
begin
    for numName in curFor('女') 
        loop
            dbms_output.put_line('学号:' || numName.sNum || ',姓名:' || numName.sName);
        end loop;
end;


运行提示以下错误:
   
第 11 行出现错误:
ORA-06550: 第 11 行, 第 45 列:
PLS-00302: 必须说明 'SNUM' 组件
ORA-06550: 第 11 行, 第 4 列:
PL/SQL: Statement ignored

提示是sNum没有定义。但sNum在记录stuRecordFor中定义为sNum MyStudent.stu_num%type了呀?为什么还会提示错误?

------解决方案--------------------
在for循环中,numname时循环索引,不需要声明的

declare 
cursor curFor(varSex varchar2) is select stu_num, stu_name from MyStudent where stu_sex = varSex;
begin
for numName in curFor('女') 
loop
dbms_output.put_line('学号:' || numName.stu_num || ',姓名:' || numName.stu_name);
end loop;
end;