Skip to content

Generate Truncate Table Statements (truncate.gen)

This generator script creates TRUNCATE TABLE statements for tables matching the specified criteria. TRUNCATE is a DDL operation that removes all rows from a table quickly and cannot be rolled back.

rem truncate.gen
rem
select 'truncate table ' || owner || '.' || table_name || ';'
from sys.dba_tables
where owner like upper('&owner')
and table_name like upper('&table')
order by owner, table_name;
-- Basic usage
@truncate.gen
-- When prompted, enter:
-- owner: Schema owner pattern (use % for wildcard)
-- table: Table name pattern (use % for wildcard)

The script prompts for:

  • &owner - Schema owner pattern (use % for wildcard)
  • &table - Table name pattern (use % for wildcard)
SELECT ANY DICTIONARY
-- OR --
SELECT ON DBA_TABLES
truncate table HR.EMPLOYEES;
truncate table HR.EMPLOYEES_TEMP;
truncate table HR.JOBS_HISTORY;
  • TRUNCATE vs DELETE: TRUNCATE is faster but cannot be rolled back
  • Foreign Keys: Tables with referential constraints may fail to truncate
  • Triggers: TRUNCATE does not fire DELETE triggers
  • High Water Mark: TRUNCATE resets the table’s high water mark

Truncate All Tables in Schema

@truncate.gen
-- Enter: SCOTT for owner, % for table

Truncate Specific Table Pattern

@truncate.gen
-- Enter: % for owner, %_TEMP for table