Created
June 28, 2017 17:43
-
-
Save jasongaylord/4043dcf62622c7006bb19ccef8829ef5 to your computer and use it in GitHub Desktop.
Cleans all objects from a database except the procedure itself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create proc Cleanup | |
as | |
declare @n char(1) | |
set @n = char(10) | |
declare @stmt nvarchar(max) | |
-- procedures | |
select @stmt = isnull( @stmt + @n, '' ) + | |
'drop procedure [' + schema_name(schema_id) + '].[' + name + ']' | |
from sys.procedures | |
where name <> 'Cleanup' | |
-- check constraints | |
select @stmt = isnull( @stmt + @n, '' ) + | |
'alter table [' + schema_name(schema_id) + '].[' + object_name( parent_object_id ) + '] drop constraint [' + name + ']' | |
from sys.check_constraints | |
-- functions | |
select @stmt = isnull( @stmt + @n, '' ) + | |
'drop function [' + schema_name(schema_id) + '].[' + name + ']' | |
from sys.objects | |
where type in ( 'FN', 'IF', 'TF' ) | |
-- views | |
select @stmt = isnull( @stmt + @n, '' ) + | |
'drop view [' + schema_name(schema_id) + '].[' + name + ']' | |
from sys.views | |
-- foreign keys | |
select @stmt = isnull( @stmt + @n, '' ) + | |
'alter table [' + schema_name(schema_id) + '].[' + object_name( parent_object_id ) + '] drop constraint [' + name + ']' | |
from sys.foreign_keys | |
-- tables | |
select @stmt = isnull( @stmt + @n, '' ) + | |
'drop table [' + schema_name(schema_id) + '].[' + name + ']' | |
from sys.tables | |
-- user defined types | |
select @stmt = isnull( @stmt + @n, '' ) + | |
'drop type [' + schema_name(schema_id) + '].[' + name + ']' | |
from sys.types | |
where is_user_defined = 1 | |
exec sp_executesql @stmt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment