Uncategorized

Drop all schemas in database (WHILE version)

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON
GO

DECLARE @SchemaName NVARCHAR(128)
DECLARE @Stmt NVARCHAR(MAX)

DECLARE @SchemasToDel TABLE ([SchemaName] NVARCHAR(128) NOT NULL PRIMARY KEY)

INSERT INTO @SchemasToDel 
	( [SchemaName] )
SELECT [s].[name]
FROM [sys].[schemas] [s]
WHERE [s].[name] IN (/*Fill list*/)

WHILE EXISTS (SELECT * FROM @SchemasToDel)
BEGIN
    
	SELECT TOP(1)
		@SchemaName = [SchemaName]
	FROM @SchemasToDel
	ORDER BY [SchemaName]

	PRINT 'DROP SCHEMA ' + QUOTENAME(@SchemaName) + ';' + CHAR(13) + 'GO'

	DELETE FROM @SchemasToDel WHERE [SchemaName] = @SchemaName

END
GO