如何返回超过1条记录?

时间:2014-01-21 17:36:40

标签: oracle plsql procedure

create or replace procedure p_inout
  (v_emp_lname in varchar2)
  as
  v_first_name varchar2(20);
begin
  select first_name into v_first_name
  from employees
  where last_name=v_emp_lname;
  dbms_output.put_line(v_first_name);
end p_inout;
/

以上代码用于在输入此人的姓氏时返回名字。 但是有许多重复的姓氏。当我试图运行查询时,我收到了这个错误。

ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "HR.P_INOUT", line 6
ORA-06512: at line 1
01422. 00000 -  "exact fetch returns more than requested number of rows"
*Cause:    The number specified in exact fetch is less than the rows returned.
*Action:   Rewrite the query or change number of rows requested

如何返回多于记录?

1 个答案:

答案 0 :(得分:2)

您选择了一个标量变量v_first_name,它显然只能存储一个值,因此在这种情况下选择多行是没有意义的。如果可能有许多具有相同姓氏的记录,请尝试以下操作:

create or replace procedure p_inout
  (v_emp_lname in varchar2)
  as
begin
  for rec in
  (
    select first_name
    from employees
    where last_name=v_emp_lname
  )
  loop
    dbms_output.put_line(rec.first_name);
  end loop;
end p_inout;
/