Conditionally define a Cursor

后端 未结 3 1963
鱼传尺愫
鱼传尺愫 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条回答
  •  梦毁少年i
    2020-12-19 08:01

    I would probably code something like this (where the two loops may call the same functions)

    BEGIN
      IF( v_action = 'DO THIS' )
      THEN
        FOR this_cur IN (SELECT * FROM )
        LOOP
          <>
        END LOOP;
      ELSE
        FOR that_cur IN (SELECT * FROM )
        LOOP
          <>
        END LOOP;
      END IF;
    END;
    

    You could also use dynamic SQL to open the cursor but that tends to get more complicated, particularly if there are only two options.

    IS
      get_records SYS_REFCURSOR;
      l_sql_stmt  VARCHAR2(100);
    BEGIN
      IF( v_action = 'DO THIS' )
      THEN
        l_sql_stmt := 'SELECT * from ';
      ELSE
        l_sql_stmt := 'SELECT * from ';
      END IF;
    
      OPEN get_records FOR l_sql_stmt;
      ...
    

提交回复
热议问题