Conditionally define a Cursor

后端 未结 3 1961
鱼传尺愫
鱼传尺愫 2020-12-19 07:34

I have a Procedure in Oracle that takes a varchar2 paramater. Based on the value of that parameter, I need to define a cursor. The cursor will operate on diff

3条回答
  •  甜味超标
    2020-12-19 07:47

    you will need a REF CURSOR and open it conditionaly, for example:

    SQL> CREATE OR REPLACE PROCEDURE GET_RECORDS(v_action IN VARCHAR2) IS
      2     v_thing     VARCHAR2(10);
      3     get_records SYS_REFCURSOR;
      4  BEGIN
      5     IF (v_action = 'DO THIS') THEN
      6        OPEN get_records FOR
      7           SELECT 1 FROM dual;
      8     ELSE
      9        OPEN get_records FOR
     10           SELECT 2 FROM dual;
     11     END IF;
     12  
     13     LOOP
     14        FETCH get_records INTO v_thing;
     15        EXIT WHEN get_records%NOTFOUND;
     16        /* do things */
     17        dbms_output.put_line(v_thing);
     18     END LOOP;
     19     CLOSE get_records;
     20  END;
     21  /
    
    Procedure created
    
    SQL> exec get_records ('DO THIS');
    1
    
    PL/SQL procedure successfully completed
    
    SQL> exec get_records ('DO THAT');
    2
    
    PL/SQL procedure successfully completed
    

提交回复
热议问题