2013-02-27 2 views
1

REFCURSOR를 받고 데이터 조작을 수행하는 함수를 실행하려고합니다.RefCursor를 함수에 전달할 때 커서가 유효하지 않습니다.

var some_cursor REFCURSOR; 

exec :some_cursor := SCHEMA.test_getcursor; 

print some_cursor; 

variable res varchar2; 

exec :res := SCHEMA.second_function(:some_cursor, 'Other_parameter'); 

print res; 

지금, 처음 test_getcursor 기능, 커서를 엽니 다 간단한 함수 인 선택 쿼리를 수행하고 커서를 반환

내가이 SQL/PLUS 코드가 제 기능을 테스트합니다. 그것은 잘 작동하며 some_cursor도 잘 인쇄됩니다.

second_function을 호출하고 refcursor를 전달하면 문제가 발생합니다.

type cursor_row 
IS RECORD 
(field_1 some_field1%type, 
field_2 some_field2%type, 
field_3 some_field3%type); 

new_row cursor_row; 

BEGIN 
LOOP 
fetch PASSED_IN_REFCURSOR INTO new_row --this is where the function fails 
...data manipulation... 
EXIT WHEN PASSED_IN_REFCURSOR%NOTFOUND; 
END LOOP; 
CLOSE PASSED_IN_REFCURSOR; 
END; 

내가 점점 오전 오류가 Invalid Cursor입니다 :

기능은 다음과 같은 코드가 있습니다.

내가 만든 유형이 참조 커서와 동일한 수의 행과 데이터 유형을 가지고 있다고 확신합니다.

이 경우 내가 뭘 잘못하고 있니?

print some_cursor; 

이 제거 : 나는, PL/SQL 10.2

답변

3

을 오라클 10g를이 방법으로 문제가되어 사용하고 있습니다. 커서를 인쇄하면 모든 레코드를 가져 와서 닫았습니다. 그래서 두 번째 함수는 더 이상 읽을 수 없습니다.

;

SQL> create procedure two(p_rc sys_refcursor) 
    2 is 
    3 v_col varchar2(1); 
    4 begin 
    5 loop 
    6  fetch p_rc into v_col; 
    7  exit when p_rc%notfound; 
    8  dbms_output.put_line(v_col); 
    9 end loop; 
10 end; 
11/

Procedure created. 

SQL> set serverout on 
SQL> var rc refcursor; 
SQL> exec :rc := one; 

PL/SQL procedure successfully completed. 

SQL> exec two(:rc); 
X 

PL/SQL procedure successfully completed. 

SQL> var rc refcursor; 
SQL> exec :rc := one; 

PL/SQL procedure successfully completed. 

SQL> print rc 

D 
- 
X 

SQL> exec two(:rc); 
BEGIN two(:rc); END; 

* 
ERROR at line 1: 
ORA-01001: invalid cursor 
ORA-06512: at "TEST.TWO", line 6 
ORA-06512: at line 1 

대 한에 폭탄 밖으로 커서가 열려 있지으로 가져옵니다. 먼저 %isopen을 확인하고보다 유용한 오류를 되돌리려면 정의 된 오류를 발생시켜야합니다.

+0

함수와 저장 프로 시저의 사용에는 차이가 있습니까? 함수를 사용하고 똑같은 동작을하는 것처럼 보입니다.'Error report : Cursor is closed '라고 다시 돌아옵니다. – Victor

관련 문제