PL / SQL选择内部循环

时间:2018-05-07 10:12:49

标签: oracle plsql

我有一个PL / SQL语句,它应该迭代select语句,执行存储过程,然后从表中选择一些东西。

该陈述如下所示:

BEGIN
    FOR r IN (
        select org_id ,name from table2 order by 1
    ) LOOP

        dbms_application_info.set_cl (r.org_id);
        Select prj_id, prj_name 
        INTO l_output
        FROM table1
        WHERE CREATION_DATE>TO_DATE('10/01/2017','DD-MM-YYYY');   
        dbms_sql.return_result(l_output); --error     
    END LOOP;
END;

我不知道如何显示查询返回的所有行。有人可以帮我解决这个问题吗?我搜索了一个解决方案,但直到现在都没有运气..

PS:Table1是一个需要在select

之前启动存储过程的视图

提前致谢!

3 个答案:

答案 0 :(得分:2)

过程dbms_sql.return_result接受/返回引用游标,您需要在调用此过程之前打开引用游标:

    DECLARE
      l_output sys_refcursor;    
    BEGIN
      FOR r IN (SELECT org_id,
                       NAME
                  FROM table2
                 ORDER BY 1) LOOP

        dbms_application_info.set_cl(r.org_id);
        OPEN l_output FOR
          SELECT prj_id,
                 prj_name
            FROM table1
           WHERE creation_date > to_date('10/01/2017', 'DD-MM-YYYY');
        dbms_sql.return_result(l_output);
      END LOOP;
    END;

答案 1 :(得分:1)

所以也许使用与外部查询相同的结构?

BEGIN
    FOR r IN (
        select org_id ,name from table2 order by 1
    ) LOOP

        dbms_application_info.set_cl (r.org_id);

        FOR inner_query IN (
          Select prj_id, prj_name 
          FROM table1
          WHERE CREATION_DATE>TO_DATE('10/01/2017','DD-MM-YYYY');   
        ) LOOP
          dbms_output.put_line('prj_id: ' || inner_query.prj_id || ' prj_name:' || inner_query.prj_name);
        END LOOP;  
    END LOOP;
END;

修改 如果你想通过dbms_sql.return_result从你的过程隐式返回游标,你应该将变量声明为ref_cursor:

l_output SYS_REFCURSOR

然后打开它查询:

 OPEN l_output FOR SELECT ...

最后通过以下程序返回:

 DBMS_SQL.RETURN_RESULT(l_output);

答案 2 :(得分:0)

BEGIN
    FOR r IN (select org_id ,name from table2 order by 1)
       LOOP
          dbms_application_info.set_cl (r.org_id);
         for x in (Select prj_id, prj_name
                    FROM table1
                   WHERE CREATION_DATE>TO_DATE('10/01/2017','DD-MM-YYYY'))
            loop   
              dbms_output.put_line('prj_id:  '||x.prj_id ||'  prj_name:  '||x.prj_name );
         end loop;     
    END LOOP;
END;