Menu

SQL Server delete / drop all tables under special schema

The project I am working on had migrations issues – it simply didn’t do the database UPDATE automatically. I have fixed the issue but had to make sure the automatic migrations are working as expected.

Needed to test many times, to see if it create it automatically was run and creates tables, SPs, Views, etc. In order to do that I need to DROP table few times

DECLARE @SqlStatement NVARCHAR(MAX)
SELECT @SqlStatement = 
COALESCE(@SqlStatement, N'') + N'DROP TABLE [MYSCHEMA].' + QUOTENAME(TABLE_NAME) + N';' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'MYSCHEMA' and TABLE_TYPE = 'BASE TABLE'

PRINT @SqlStatement

Now copy the statements and run it.

Leave a comment