问题
I want to loop results from stored procedure. My code:
set serveroutput on
VAR c_curs refcursor;
EXECUTE pck_prov.get_value_type_list(1, :c_curs);
BEGIN
FOR record_test IN c_curs LOOP
dbms_output.put_line(record_test.id);
END LOOP;
END;
I don't understand why this is throwing error that c_curs must be declared:
Error starting at line : 7 in command - BEGIN
FOR record_test IN c_curs LOOP dbms_output.put_line(record_test.id); END LOOP;
END;
Error report -ORA-06550: line 2, column 24:
PLS-00201: identifier 'C_CURS' must be declared
回答1:
The cursor can be reference in a PL/SQL block as follows:
set serveroutput on
DECLARE
c_curs SYS_REFCURSOR;
v_id NUMBER;
BEGIN
pck_prov.get_value_type_list (1, c_curs); --> procedure called here
LOOP
FETCH c_curs
INTO v_id;
EXIT WHEN c_curs%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_id);
END LOOP;
CLOSE c_curs;
END;
/
回答2:
c_curs
is a bind variable and is not a PL/SQL defined variable. To use it in a PL/SQL block you need to prefix it with a colon :
to indicate that you are using a bind variable (exactly as you did in the EXECUTE
statement):
set serveroutput on
VAR c_curs refcursor;
EXECUTE pck_prov.get_value_type_list(1, :c_curs);
BEGIN
FOR record_test IN :c_curs LOOP
dbms_output.put_line(record_test.id);
END LOOP;
END;
来源:https://stackoverflow.com/questions/61308994/looping-cursor-throws-error-that-cursor-is-not-defined