Skip to Main Content
  • Questions
  • How to create dynamic table type and variable to insert bulk data

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Alok.

Asked: June 05, 2017 - 11:06 am UTC

Last updated: June 05, 2017 - 2:51 pm UTC

Version: Latest

Viewed 10K+ times! This question is

You Asked

Hi Tom,

I have a scenario in which I need to pass the table name as input variable and create a table type based on that table and insert the data into
input table using bulk collect dynamically.I have tried creating the table type and insertion as below:
       DECLARE
       TYPE tabtype IS TABLE OF pTableName%ROWTYPE 
           INDEX BY PLS_INTEGER;   

       TYPE numtype IS TABLE OF NUMBER 
           INDEX BY PLS_INTEGER; 

        raRec           tabtype;

        BEGIN 
            FORALL i IN raTab.FIRST .. raTab.LAST-- SAVE EXCEPTIONS

            EXECUTE IMMEDIATE   'INSERT INTO ' || pTableName || 
                  ' VALUES ' || raTab (i); 
                  
                       
         EXCEPTION 
            WHEN OTHERS 
            THEN 
               NULL; 
         END;


But its throwing error when compiled in a procedure.

Thanks in advance.

and Chris said...

The short answer is:

You can't. You can only use %rowtype on fixed objects (tables, predefined cursors, etc.)

The long answer is:

If you have generic cursors with unknown/variable columns you can use DBMS_SQL to define the columns and fetch the values.

Connor has a brief example at:

https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:9525670000346741460

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library