下面的代码就是一个功能,在数据库查询按年龄查询,如果查询到多条数据的话,就引发一个异常,这个没问题,但是如果返回结果不大于1的话,我的else的这个语句总是提示我有错误,错误提示如下:
***************************************************
ERROR 位于第 10 行:
ORA-06550: 第 10 行, 第 1 列:
PLS-00428: 在此 SELECT 语句中缺少 INTO 子句***************************************************
declare
return_res number(4);
in_age number(10);
begin
in_age := &in_age;
select count(*) into return_res from stu where sage=in_age;
if return_res > 1 then
raise too_many_rows;
else
open for select * from stu where sage=in_age;
end if;
exception
when too_many_rows then
dbms_output.put_line('返回多行');
end;
/

解决方案 »

  1.   

    修改成下列形式应该就可以了(使用你的PL/SQL 我不能得到你的错误信息)
    declare
    return_res number(4);
    in_age number(10);TYPE type_cv  IS REF CURSOR;
       cv_c      type_cv;  
    begin
    in_age := &in_age;
    select count(*) into return_res from stu where sage=in_age;
    if return_res > 1 then
    raise too_many_rows;
    else
    open cv_c for select * from stu where sage=in_age;
    end if;
    exception
    when too_many_rows then
    dbms_output.put_line('返回多行');
    end;
      

  2.   

    open   for   select   *   from   stu   where   sage=in_age; 
     ====>open 后面没cursor
      

  3.   

    如果返回结果不大于1的话,else的这个语句中的return_res估计取不到值,所以会出错.