Created
November 29, 2018 21:10
-
-
Save jesslilly/c95793ca0cb113818c56aa4058733f09 to your computer and use it in GitHub Desktop.
DROP ALL SQL SERVER TABLES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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