Skip to content

Instantly share code, notes, and snippets.

@justinpitts
Created September 28, 2011 17:26
Show Gist options
  • Save justinpitts/1248570 to your computer and use it in GitHub Desktop.
Save justinpitts/1248570 to your computer and use it in GitHub Desktop.
partial fragment to emit a "drop all objects in schema" statement in TSQL
WITH
TABLES AS ( select TABLE_SCHEMA, TABLE_NAME , case table_type when 'view' then 'view' else 'table' end as type from INFORMATION_SCHEMA.TABLES)
, procs as ( select ROUTINE_SCHEMA, routine_name, ROUTINE_TYPE from INFORMATION_SCHEMA.ROUTINES )
, domains as (select DOMAIN_SCHEMA, DOMAIN_NAME, 'TYPE' as type, '[' + DOMAIN_SCHEMA + '].[' + DOMAIN_NAME + ']' as q_name from INFORMATION_SCHEMA.DOMAINS)
, c as ( select '[' + table_schema + '].[' + table_name + ']' as q_name, type from ( select * from tables union select * from procs ) a )
select 'if object_id(''' + q_name + ''') is not null drop ' + type + ' ' + q_name +';' from c union select 'drop ' + type + ' ' + q_name +';' from domains
@justinpitts
Copy link
Author

Actually, as it stands, it emits drops for all table/view/proc/function/schemata in the current database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment