sql >> Base de Datos >  >> RDS >> Oracle

Recopilación masiva y ejecución inmediata en Oracle

Sí, técnicamente puedes:

  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 

Y Oracle lo establece claramente en la documentación:

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

Pero puede usar un evento de forma más útil si realmente NECESITA ejecutar SQL dinámico:cursores de referencia débiles. Tendrá acceso a una opción tan poderosa como LIMIT y podrá usar colecciones de registros.

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