Sql 通过存储过程显示数据
您好,我正在尝试使用存储过程显示表中的数据。姓氏作为参数通过存储过程传递,执行时,存储过程应显示具有姓氏的所有行。这是我得到的错误;请帮忙-Sql 通过存储过程显示数据,sql,oracle,select,stored-procedures,Sql,Oracle,Select,Stored Procedures,您好,我正在尝试使用存储过程显示表中的数据。姓氏作为参数通过存储过程传递,执行时,存储过程应显示具有姓氏的所有行。这是我得到的错误;请帮忙- Create or replace procedure disp(pEMPLASTNAME varchar2) IS Row employee%rowtype; begin select * into row from employee where EMPLASTNAME=’pEMPLASTNAME’ ; dbms_output.put_line('Nam
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
但是,无论如何,您可能没有该变量值的任何数据(例如,某一天,它可能会发生)
这就是为什么我建议您捕获异常并处理它(请参见exception
block)
pd:最好不要使用保留字,如
行
。我建议您以另一种方式命名变量。我建议使用光标选择所有行,然后通过光标循环打印结果:
select * into row from employee where EMPLASTNAME=pEMPLASTNAME;
ORA-01422:精确抓取返回的数量超过请求的数量rows@Nidhin_toms,这意味着不止一名员工姓“Mark”。此pl/sql块只允许返回一行。
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;
/