Skip to content

Instantly share code, notes, and snippets.

@idkw
Last active April 13, 2016 11:58
Show Gist options
  • Save idkw/b57d42113ac0d318a22bf4b9d7614da4 to your computer and use it in GitHub Desktop.
Save idkw/b57d42113ac0d318a22bf4b9d7614da4 to your computer and use it in GitHub Desktop.
Drop all tables from a SQL Server schema (uncomment -- EXEC to apply changes)
---------------------------------------------
-- SQLServer : DROP all tables from SCHEMA_NAME
---------------------------------------------
DECLARE @SCHEMA_NAME varchar(100)
SET @SCHEMA_NAME = 'my_schema'
DECLARE @cmd varchar(4000)
DECLARE MY_CURSOR CURSOR
LOCAL STATIC READ_ONLY FORWARD_ONLY
FOR
select 'DROP TABLE ['+s.name+'].['+t.name+'];' FROM sys.Tables t INNER JOIN sys.schemas s ON s.schema_id = t.schema_id WHERE s.name = @SCHEMA_NAME
OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @cmd
WHILE @@FETCH_STATUS = 0
BEGIN
-- EXEC (@cmd)
PRINT @cmd
FETCH NEXT FROM MY_CURSOR INTO @cmd
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment