I want to go through all tables and copy the IDs and the tablename into a table. Since I have to use a variable as a table name, I tried it with IMMEDIATE EXECUTE. But with dynamic SQL the INSERT INTO statement allows only single rows.
The following SQL Statement would be it, if IMMEDIATE EXECUTE would allow INSERT INTO with muplitple rows.
DECLARE
sqlStat VARCHAR2(500);
BEGIN
FOR TName IN (
SELECT TABLE_NAME FROM all_tab_cols WHERE column_name='ID'
)
LOOP
sqlStat := 'INSERT INTO storeTab (ID,TABLE_NAME) SELECT ID, '' :1 '' FROM :2';
EXECUTE IMMEDIATE sqlStat USING TName.TABLE_NAME,TName.TABLE_NAME;
END LOOP;
END;
How can I loop through tables and collect records?