Last active
June 17, 2021 08:05
-
-
Save peterkeller/2c92498d141a89f3c90baf64211b010d to your computer and use it in GitHub Desktop.
Drop all tables in MS SQL Server
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
-- https://www.mssqltips.com/sqlservertip/6798/drop-all-tables-sql-server/ | |
USE [BikeStores] | |
GO | |
-- drop constraints | |
DECLARE @DropConstraints NVARCHAR(max) = '' | |
SELECT @DropConstraints += 'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' | |
+ QUOTENAME(OBJECT_NAME(parent_object_id)) + ' ' + 'DROP CONSTRAINT' + QUOTENAME(name) | |
FROM sys.foreign_keys | |
EXECUTE sp_executesql @DropConstraints; | |
GO | |
-- drop tables | |
DECLARE @DropTables NVARCHAR(max) = '' | |
SELECT @DropTables += 'DROP TABLE ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) | |
FROM INFORMATION_SCHEMA.TABLES | |
EXECUTE sp_executesql @DropTables; | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment