Looping cursor throws error that cursor is not defined

主宰稳场 提交于 2021-02-10 16:55:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!