简体   繁体   English

Oracle中的“批量收集”和“立即执行”

[英]“Bulk Collect Into” and “Execute Immediate” in Oracle

is it possible to execute the "bulk Collect into" with the "execute immediate" commands in oracle? 是否可以使用oracle中的"execute immediate"命令执行"bulk Collect into" All of that would be part of a function that returns a pipe lined table as a result. 所有这些都将成为返回管道表的函数的一部分。

Yes, technically you can: 是的,从技术上讲,你可以:

  1  SQL> declare
  2   type x is table of t.id%type index by pls_integer;
  3   xx x;
  4  begin
  5   execute immediate
  6   'select id from t' bulk collect into xx;
  7   dbms_output.put_line(xx.count);
  8  end;
  9  /
426 

And Oracle clearly states this in the documentation: Oracle在文档中明确说明了这一点:

http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/executeimmediate_statement.htm http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/executeimmediate_statement.htm

But you can use more useful way event if you really NEED to execute Dynamic SQL - weak ref cursors. 但是,如果您真的需要执行动态SQL,那么您可以使用更有用的方式事件 - 弱ref游标。 You will have the access to such powerful option as LIMIT and will be able to use collections of records. 您可以访问LIMIT这样强大的选项,并且可以使用记录集合。

SQL> declare
  2   type x is table of t%rowtype index by pls_integer;
  3   xx x;
  4   c sys_refcursor;
  5  begin
  6    open c for 'select * from t';
  7    loop
  8      fetch c bulk collect into xx limit 100;
  9      dbms_output.put_line(xx.count);
 10      exit when c%notfound;
 11    end loop;
 12    close c;
 13  end;
 14  /
100                                                                             
100                                                                             
100                                                                             
100                                                                             
26   

Following the idea proposed by Dmitry Nikiforov I solved the problem using cursors and here is the solution ;) 遵循Dmitry Nikiforov提出的想法,我使用游标解决了问题,这是解决方案;)

FUNCTION myFunction (  parameter_p IN VARCHAR2) RETURN myTableType PIPELINED

IS
  c sys_refcursor;
  stmt varchar2(4000);  
  out_rec mYrowType;
  rec mYrowType;
BEGIN

    stmt:='Select * from ''' || parameter_p || '''';
    open c for stmt;

    LOOP
    fetch c into rec;
    EXIT WHEN c%NOTFOUND;

        out_rec.field1 := rec.field1;
        out_rec.field2 := rec.field2;
        out_rec.field3 := rec.field3;

        PIPE Row(out_rec);
    END LOOP;

  Close c;
  Return;

END myFunction;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM