2014-01-21 3 views
2
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 

레코드보다 많은 데이터를 반환하려면 어떻게합니까?

+0

v_first_name 변수를 사용할 특별한 이유가 있습니까? – Dee

+0

커서를 참조하십시오. http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/cursor_for_loop_statement.htm#LNPLS1155 – otisonoza

답변

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; 
/
+0

굉장, 작동합니다. – JohnD

관련 문제