Oracle SQL

How to delete all table except database ?

How to delete all table except database :

Step 1:

Print the deleted all table for below query:

DECLARE sql_query VARCHAR2(100);
BEGIN
FOR table_rec IN (SELECT table_name
FROM all_tables
WHERE owner = 'Your_Selected_Schema') LOOP
sql_query := 'DROP TABLE ' || table_rec.table_name || ' CASCADE CONSTRAINTS';
DBMS_OUTPUT.PUT_LINE(sql_query);
END LOOP;
END;

Note: Your_Selected_Schema Must be Capital.

Step 2:

Execute the deleted all table for below query:

DECLARE sql_query VARCHAR2(100);
BEGIN
FOR table_rec IN (SELECT table_name
FROM all_tables
WHERE owner = 'Your_Selected_Schema') LOOP
sql_query := 'DROP TABLE ' || table_rec.table_name || ' CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE sql_query;
END LOOP;
END;

About the author

shohal

Leave a Comment