Skip to content

Instantly share code, notes, and snippets.

@peterkeller
Last active June 17, 2021 08:05
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 peterkeller/2c92498d141a89f3c90baf64211b010d to your computer and use it in GitHub Desktop.
Save peterkeller/2c92498d141a89f3c90baf64211b010d to your computer and use it in GitHub Desktop.
Drop all tables in MS SQL Server
-- 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