declare
str number(10);
va1 varchar2(10);
va2 int;
va3 varchar2(10);
type cur_s is ref cursor;
cur cur_s;
except1 exception;
begin
     str:=#
     open cur for select b.cla_name into va1,c.sce_score into va2,a.stu_name into va3 from student a,class b,score c where a.stu_id in(select c.sce_stuid from c where c.sce_claid=str) and c.sce_stuid=a.stu_id and c.sce_claid=b.cla_id;
     loop
         if cur%found then
         dbms_output.put_line('查询结果为:');
         dbms_output.put_line('班级名: '||va1);
         dbms_output.put_line('学生名: '||va3);
         dbms_output.put_line('成绩: '||va2);
         elsif cur%notfound then
         raise except1;
         end if;
     end loop;
     exception
              when except1 then
              dbms_output.put_line('没有或没有找到该班的任何学员!');
     close cur;
end;

解决方案 »

  1.   

    加了!但是还是错啊~?
    错误代码:
    declare
        type t_rec is record
        (
            var1 student.stu_name%type,
            var2 class.cla_name%type,
            var3 score.sce_score%type
        );
        rec t_rec;
    str number(10);
    type cur_s is ref cursor return t_rec;
    cur cur_s;
    except1 exception;
    begin
         str:=#
         open cur for select b.cla_name,c.sce_score,a.stu_name from student a,class b,score c where a.stu_id in(select sce_stuid from score where sce_claid=str) and c.sce_stuid=a.stu_id and c.sce_claid=b.cla_id;
         loop
             if cur%found then
             fetch cur into rec;
             dbms_output.put_line('查询结果为:');
             dbms_output.put_line('班级名: '||rec.var2);
             dbms_output.put_line('学生名: '||rec.var1);
             dbms_output.put_line('成绩: '||rec.var3);
             elsif cur%notfound then
             raise except1;
             end if;
         end loop;
         exception
                  when except1 then
                  dbms_output.put_line('没有或没有找到该班的任何学员!');
         close cur;
    end;
      

  2.   

    在 loop  前面 加 fetch cur into rec; 把if cur%found then
             fetch cur into rec;
             dbms_output.put_line('查询结果为:');
             dbms_output.put_line('班级名: '||rec.var2);
             dbms_output.put_line('学生名: '||rec.var1);
             dbms_output.put_line('成绩: '||rec.var3);
    改成 
    if cur%found then
             dbms_output.put_line('查询结果为:');
             dbms_output.put_line('班级名: '||rec.var2);
             dbms_output.put_line('学生名: '||rec.var1);
             dbms_output.put_line('成绩: '||rec.var3);
             fetch cur into rec;这样试一下
      

  3.   

    试试  改成 while  loop的形式
      

  4.   

    可以了!但怎么查不出记录?
    declare
        type t_rec is record
        (
            var1 student.stu_name%type,
            var2 class.cla_name%type,
            var3 score.sce_score%type
        );
        rec t_rec;
    str number(10);
    type cur_s is ref cursor return t_rec;
    cur cur_s;
    except1 exception;
    begin
         str:=#
         open cur for select b.cla_name,c.sce_score,a.stu_name from student a,class b,score c where a.stu_id in(select sce_stuid from score where sce_claid=str) and c.sce_stuid=a.stu_id and c.sce_claid=b.cla_id;
         while cur%found
         loop 
             fetch cur into rec;
             dbms_output.put_line('查询结果为:');
             dbms_output.put_line('班级名: '||rec.var2);
             dbms_output.put_line('学生名: '||rec.var1);
             dbms_output.put_line('成绩: '||rec.var3);
         end loop;
         exception
                  when except1 then
                  dbms_output.put_line('没有或没有找到该班的任何学员!');
         close cur;
    end;
      

  5.   

    这样都不行~?记录为空。
    declare
        type t_rec is record
        (
            var1 student.stu_name%type,
            var2 class.cla_name%type,
            var3 score.sce_score%type
        );
        rec t_rec;
    str number(10);
    type cur_s is ref cursor return t_rec;
    cur cur_s;
    begin
         str:=#
         open cur for select b.cla_name,c.sce_score,a.stu_name from student a,class b,score c where a.stu_id in(select sce_stuid from score where sce_claid=str) and c.sce_stuid=a.stu_id and c.sce_claid=b.cla_id;
         while cur%found
         loop 
             fetch cur into rec;
             dbms_output.put_line('查询结果为:');
             dbms_output.put_line('班级名: '||rec.var2);
             dbms_output.put_line('学生名: '||rec.var1);
             dbms_output.put_line('成绩: '||rec.var3);
         end loop;
         exception
                  when NO_DATA_FOUND then
                  dbms_output.put_line('没有或没有找到该班的任何学员!');
         close cur;
    end;