oracle out ref游标循环

时间:2018-10-03 14:57:41

标签: oracle loops cursor

如何将输出光标 o_cur 的“值”传递到另一个循环?

CREATE OR REPLACE PROCEDURE dyn_cursor (o_cur OUT SYS_REFCURSOR)
IS
    script   VARCHAR2 (4000);
BEGIN
    script := 'select sysdate-1 notnow, sysdate today from dual union all select sysdate+1 notnow, sysdate today from dual';
    OPEN o_cur FOR script;

-- the question is related to this block:   
    for i in o_cur
    loop
        DBMS_OUTPUT.PUT_LINE(i.notnow);
    end loop;
-----------------------

END;

3 个答案:

答案 0 :(得分:1)

FETCH通常在过程中在反射器是OUT变量的情况下处理

CREATE OR REPLACE PROCEDURE dyn_cursor (o_cur OUT SYS_REFCURSOR)
IS
    script   VARCHAR2 (4000);
BEGIN
    script := 'select sysdate-1 notnow, sysdate today from dual union all select sysdate+1 notnow, sysdate today from dual';
    OPEN o_cur FOR script;

END;
/

SET SERVEROUTPUT ON
DECLARE
     v_cur      SYS_REFCURSOR;
     v_notnow   DATE;
     v_today    DATE;
BEGIN
     dyn_cursor(v_cur);
     LOOP
          FETCH v_cur INTO
               v_notnow,
               v_today;
          EXIT WHEN v_cur%notfound;
          dbms_output.put_line(v_notnow);
     END LOOP;
END;
/


02-10-18
04-10-18


PL/SQL procedure successfully completed.

答案 1 :(得分:0)

这似乎是一个奇怪的构造。我会创建一个视图,可以在不同的地方重复使用。

CREATE OR REPLACE VIEW VW_Times AS
    SELECT sysdate-1 notnow, sysdate today FROM dual
    UNION ALL
    SELECT sysdate+1 notnow, sysdate today FROM dual;

CREATE OR REPLACE PROCEDURE OutputTime ()
IS
   CURSOR o_cur IS
     SELECT * FROM VW_Times;

BEGIN
   FOR i IN o_cur
   LOOP
      DBMS_OUTPUT.PUT_LINE(i.notnow);
   END LOOP;
END;

答案 2 :(得分:0)

Kaushik Nayak上发帖,我找到了可以接受的解决方案:

CREATE OR REPLACE PROCEDURE dyn_cursor (o_cur OUT SYS_REFCURSOR)
IS
    script   VARCHAR2 (4000);
    type array is table of VARCHAR2(20) index by binary_integer;
    l_datapoint1 array;
    l_datapoint2 array;
BEGIN
    script := 'select sysdate-1 notnow, sysdate today from dual union all select sysdate+1 notnow, sysdate today from dual';
    OPEN o_cur FOR script;

    LOOP
        FETCH o_cur bulk collect into l_datapoint1, l_datapoint2;
          for i in 1 .. l_datapoint1.count
          loop
              DBMS_OUTPUT.PUT_LINE( l_datapoint1(i) || ', ' || l_datapoint2(i) );
          end loop;
          exit when o_cur%notfound;
      end loop;
      close o_cur;
END;