【问题标题】:Displaying data through stored procedure通过存储过程显示数据
【发布时间】:2013-11-22 02:55:03
【问题描述】:
Create or replace procedure disp(pEMPLASTNAME varchar2)
IS
Row employee%rowtype;
begin
select * into row from employee where EMPLASTNAME=’pEMPLASTNAME’ ;
dbms_output.put_line('Name: '||Row.EMPID||' '|| Row.EMPNAME);
End;
/

BEGIN
disp(‘Mark’);
END;
/

您好,我正在尝试使用存储过程显示表中的数据。姓氏作为参数通过存储过程传递,并且在执行时,存储过程应显示所有具有姓氏的行。这是我得到的错误;请帮忙! :-

SQL> BEGIN
disp('Mark');
END;
/
BEGIN
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "TEST.DISP", line 5
ORA-06512: at line 2

【问题讨论】:

    标签: sql oracle select stored-procedures


    【解决方案1】:

    不需要引号:

    select * into row from employee where EMPLASTNAME=pEMPLASTNAME;
    

    但是,无论如何,您可能没有任何关于该变量值的数据(即有一天,它可能会发生) 这就是为什么我建议您捕获异常并对其进行处理(请参阅EXCEPTION 块)

    pd:最好不要使用保留字,例如row。我建议您以另一种方式命名您的变量。

    【讨论】:

    • ORA-01422:精确提取返回的行数超过了请求的行数
    • @Nidhin_toms,这意味着不止一名员工的姓氏像“马克”。这个 pl/sql 块只允许返回一行。
    【解决方案2】:

    我建议使用游标选择所有行,然后通过游标循环打印结果:

    Create or replace procedure disp(pEMPLASTNAME varchar2)
    IS
    Cursor row_select is
        select EMPID, EMPNAME from employee where emplastname = pEMPLASTNAME;
    -- and whatever columns you need to print, using * isn't good practice
    
    begin
    for item in row_select loop
        dbms_output.put_line('Name: '||item.EMPID||' '|| item.EMPNAME);
    end loop;
    End;
    /
    
    BEGIN
    disp(‘Mark’);
    END;
    /
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 2012-03-16
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多