"Bulk Collect Into" and "Execute Immediate" in Oracle

Thiago Burgos

is it possible to execute the "bulk Collect into" with the "execute immediate" commands in oracle? All of that would be part of a function that returns a pipe lined table as a result.

Dmitry Nikiforov

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:

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. You will have the access to such powerful option as LIMIT and will be able to use collections of records.

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   

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PLSQL Execute Immediate Dynamic Aggregate Query and Bulk Collect

Execute Immediate in oracle

Execute immediate error in Oracle

Understanding 'BULK COLLECT' in Oracle Function

oracle bulk collect and reading the data

Oracle Execute Immediate not escaping quote?

Oracle: execute immediate multiple statements

error in EXECUTE IMMEDIATE insert ORACLE

Oracle Bulk Collect into Collection using LOOP

Oracle bulk collect error PLS-00201

SELECT *DISTINCT* with Oracle CURSOR and BULK COLLECT TO

Output a string in EXECUTE IMMEDIATE in Oracle PL/SQL

Execute immediate use in select * from in oracle

How to corrrectly use oracle EXECUTE IMMEDIATE in this code

Oracle - Why is EXECUTE IMMEDIATE allowed in stored procedures?

What is the SQL Server equivalent of EXECUTE IMMEDIATE INTO in Oracle

Oracle : Delete rows with "execute immediate" and rowcount

Exception dose not working in execute immediate insert in oracle

ORACLE EXECUTE IMMEDIATE FOR LOOP ORA-06512

GRANT to EXECUTE execute immediate truncate table in Oracle

Oracle: Invalid ALTER command in execute immediate

Oracle/PLSQL Processing all data in a for LOOP after BULK COLLECT

Oracle: detect exception in type construtor during bulk collect

Getting rows that are having distinct id with bulk collect - Oracle

Please help on "BULK COLLECT" in Oracle, it runs into an infinite loop

Oracle Alter Table using Execute Immediate Not behaving as expected

Oracle PL/SQL Release 12.2.0.1.0 vs 12.1.0.2.0 - execute immediate with parameters

Missing Keyword in EXECUTE IMMEDIATE SELECT INTO Statement - Oracle PL/SQL

Create Oracle sequence via execute immediate without pipe || operator