Generate Drop Table Statements (drop_table.gen)
What This Script Does
Section titled “What This Script Does”This generator script creates DROP TABLE statements with CASCADE CONSTRAINTS and PURGE options for tables matching the specified criteria. It safely excludes system schemas and temporary tables.
The Script
Section titled “The Script”rem drop_table.genremselect 'drop table ' || owner || '.' || table_name || ' cascade constraints purge;' from sys.dba_tables where owner like upper('&owner') and table_name like upper('&table_name') and owner not in ('SYS','SYSTEM','MDSYS','CTXSYS','XDB','ORDDATA','APEX_040200') and temporary = 'N' order by owner, table_name/
-- Basic usage@drop_table.gen
-- When prompted, enter:-- owner: Schema owner pattern (use % for wildcard)-- table_name: Table name pattern (use % for wildcard)
Parameters
Section titled “Parameters”The script prompts for:
- &owner - Schema owner pattern (use % for wildcard)
- &table_name - Table name pattern (use % for wildcard)
Required Privileges
Section titled “Required Privileges”SELECT ANY DICTIONARY-- OR --SELECT ON DBA_TABLES
Sample Output
Section titled “Sample Output”drop table HR.EMPLOYEES_BACKUP cascade constraints purge;drop table HR.DEPARTMENTS_OLD cascade constraints purge;drop table SCOTT.TEST_TABLE cascade constraints purge;
Key Features
Section titled “Key Features”- CASCADE CONSTRAINTS: Automatically drops referential integrity constraints
- PURGE: Immediately releases space without going to recycle bin
- System Schema Protection: Excludes critical system schemas
- Temporary Table Exclusion: Skips temporary tables
Common Use Cases
Section titled “Common Use Cases”Drop All Tables in Schema
@drop_table.gen-- Enter: SCOTT for owner, % for table_name
Drop Tables with Specific Pattern
@drop_table.gen-- Enter: % for owner, %_BACKUP for table_name
Related Scripts
Section titled “Related Scripts”- Truncate Table Generator - Generate TRUNCATE statements
- Disable Constraints Generator - Disable constraints before operations