Skip to content

Instantly share code, notes, and snippets.

@Otterpohl
Created May 13, 2022 16:06
Show Gist options
  • Save Otterpohl/d5295d40add4c6d321aa5ea017c0af1e to your computer and use it in GitHub Desktop.
Save Otterpohl/d5295d40add4c6d321aa5ea017c0af1e to your computer and use it in GitHub Desktop.
Generate script to drop all schema objects
SET NOCOUNT ON
DECLARE @SchemaName NVARCHAR(100) = 'mipl'
SELECT 'DROP ' +
CASE WHEN type IN ('P','PC') THEN 'PROCEDURE'
WHEN type = 'U' THEN 'TABLE'
WHEN type IN ('IF','TF','FN') THEN 'FUNCTION'
WHEN type = 'V' THEN 'VIEW'
END +
' ' + QUOTENAME(SCHEMA_NAME(schema_id))+'.'+QUOTENAME(name) as DropStatement
FROM sys.objects
WHERE schema_id = SCHEMA_ID(@SchemaName)
AND type IN('P','PC','U','IF','TF','FN','V')
ORDER BY CASE WHEN type IN ('P','PC') THEN 4
WHEN type = 'U' THEN 3
WHEN type IN ('IF','TF','FN') THEN 1
WHEN type = 'V' THEN 2
END
SELECT 'DROP SCHEMA ' + QUOTENAME(@SchemaName) as DropStatement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment