Reputation: 31
I have thousands of TABLES in our database. Instead of individually creating a script to spool out all columns from the application tables. I want to loop through all the tables in alltables TABLE with tablename like 'TB_%'.
I tried to use this solution in my oracle sql developer but I am getting an error that the table does not exist. I am not a SQL expert and not familiar on how to program it in SQL. Below is the code I did:
set line 400
set pagesize 2000
set colsep |
DECLARE
CURSOR get_all_tables IS
SELECT table_name
FROM all_tables
WHERE table_name like 'TB_%';
BEGIN
FOR i IN get_all_tables
LOOP
spool 'C:\documents\script\' || i.table_name || '.txt'
execute immediate 'select * from ' || i.table_name;
spool off;
END LOOP;
END;
What I want is, for each tables found in all_tables that have table_name like 'TB_%' should create a file with spool filename equivalent to tablename. And, it should spool out all the columns for each table. Can anyone help me to check the codes I did?
Thank you
Upvotes: 0
Views: 163
Reputation: 142743
That won't work; spool
is a SQL*Plus command, you can't use it in PL/SQL.
Though, as you use SQL Developer, then use it properly.
INSERT INTO
statements in export fileCREATE TABLE
statements (you'd get INSERT INTO
as well, if you chose "Export data")Upvotes: 2