Skip to content

Instantly share code, notes, and snippets.

@jesslilly
Created November 29, 2018 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesslilly/c95793ca0cb113818c56aa4058733f09 to your computer and use it in GitHub Desktop.
Save jesslilly/c95793ca0cb113818c56aa4058733f09 to your computer and use it in GitHub Desktop.
DROP ALL SQL SERVER TABLES
BEGIN -- Delete all tables in order to re-create.
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + N'
ALTER TABLE ' + QUOTENAME(s.name) + N'.'
+ QUOTENAME(t.name) + N' DROP CONSTRAINT '
+ QUOTENAME(c.name) + ';'
FROM sys.objects AS c
INNER JOIN sys.tables AS t
ON c.parent_object_id = t.[object_id]
INNER JOIN sys.schemas AS s
ON t.[schema_id] = s.[schema_id]
WHERE c.[type] IN ('D','C','F','PK','UQ')
ORDER BY c.[type];
PRINT @sql;
EXEC sys.sp_executesql @sql;
EXEC sp_msforeachtable "DROP TABLE ?";
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment