267

I don't want to type all tables' name to drop all of them. Is it possible with one query?

Majid
  • 13,853
  • 15
  • 77
  • 113
  • 3
    Some quick googling revealed this: http://stackoverflow.com/questions/11053116/mysql-bulk-drop-table-where-table-like – JSK NS Dec 22 '14 at 16:28
  • -although (for SQLServer) this might be more useful: http://stackoverflow.com/questions/536350/drop-all-the-tables-stored-procedures-triggers-constraints-and-all-the-depend –  Dec 22 '14 at 16:30
  • 1
    With the right usernames, this could happen automatically ([obligatory xkcd link](http://xkcd.com/327/)). – Minnow Dec 22 '14 at 16:33
  • 14
    Do you have any foreign keys on tables in the database? If so, you'd need to take that into consideration, and drop those before trying to drop tables. – Anthony Grist Dec 22 '14 at 16:35
  • Keep in mind that if you have schemabound objects you can't drop the table. – Sean Lange Dec 22 '14 at 17:10
  • drop database, easy – Danillo Leão Lopes May 04 '22 at 12:43

16 Answers16

248

Use the INFORMATION_SCHEMA.TABLES view to get the list of tables. Generate Drop scripts in the select statement and drop it using Dynamic SQL:

DECLARE @sql NVARCHAR(max)=''

SELECT @sql += ' Drop table ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) + '; '
FROM   INFORMATION_SCHEMA.TABLES
WHERE  TABLE_TYPE = 'BASE TABLE'

Exec Sp_executesql @sql

Sys.Tables Version

DECLARE @sql NVARCHAR(max)=''

SELECT @sql += ' Drop table ' + QUOTENAME(s.NAME) + '.' + QUOTENAME(t.NAME) + '; '
FROM   sys.tables t
       JOIN sys.schemas s
         ON t.[schema_id] = s.[schema_id]
WHERE  t.type = 'U'

Exec sp_executesql @sql

Note: If you have any foreign Keys defined between tables then first run the below query to disable all foreign keys present in your database.

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

For more information, check here.

user247702
  • 23,641
  • 15
  • 110
  • 157
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
  • 3
    (not my downvote) ... i'd generally stick with the `[sys]` schema views if portability across rdbms' is not required. http://stackoverflow.com/a/3654313/251174 – swasheck Dec 22 '14 at 16:49
  • 1
    Also see https://sqlblog.org/blogs/aaron_bertrand/archive/2011/11/03/the-case-against-information-schema-views.aspx – Aaron Bertrand Dec 22 '14 at 17:08
  • Thanks @AaronBertrand for pointing me in right direction. Still learning all these stuff just a beginner in sql server :) Btw thanks for edit. – Pரதீப் Dec 22 '14 at 17:16
  • This is amazing - works perfectly for me. Just for my own learning, could you explain what your code is actually doing? I would be fascinated to know! thanks. – DoubleA Jan 30 '16 at 14:06
  • 2
    @DoubleA - It is very simple. First am building Drop statements for all the tables in my database and storing it into a variable. To check this you can use `Print @sql` before `exec`. Then am executing the dynamically built drop statements through `sp_executesql` – Pரதீப் Jan 30 '16 at 15:15
  • 4
    If you are using Azure, sp_msforeachtable is not available. I found this sweet nugget from @Aaron Bertrand to delete all FK constraints. Works really good with this answer. http://dba.stackexchange.com/questions/90033/how-do-i-drop-all-constraints-from-all-tables – trevorc May 06 '16 at 18:15
  • 4
    If the sp_msforeachtable is not available, you can also run the delete query multiple times, since the tables which depend on others are then deleted :) – Maarten Kieft May 02 '19 at 10:45
  • But I can't fit all this on my license plate! – Michael Sep 17 '19 at 19:13
  • If dropping external tables, will have to modify the following line of code: `SELECT @sql += ' Drop external table ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) + '; '` – openwonk Nov 06 '19 at 18:00
  • constraint check still applies after running the disabling code. – Captain Prinny Sep 29 '20 at 14:13
  • `Print @sql` has trouble printing the whole string if you have lots of tables. – CivFan Nov 25 '21 at 00:14
  • Just needs a small note on why there are "different versions" - what's the reason to choose one or the other – PandaWood Jan 17 '22 at 22:52
116

If you want to use only one SQL query to delete all tables you can use this:

EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"

This is a hidden Stored Procedure in sql server, and will be executed for each table in the database you're connected.

Note: You may need to execute the query a few times to delete all tables due to dependencies.

Note2: To avoid the first note, before running the query, first check if there foreign keys relations to any table. If there are then just disable foreign key constraint by running the query bellow:

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"
pedromendessk
  • 3,538
  • 2
  • 24
  • 36
  • 1
    I tried it on my Azure SQL database, and it didn't work. However the above answer (by Prdp) worked. – Artemious Mar 08 '17 at 22:41
  • 5
    For the side note, i need to run first command multiple times before dropped all tables but it works fine. – Alper Jan 23 '19 at 13:40
  • 1
    @Thatshowweroll that's probably because of the tables' dependencies. If one table has others that depend on it it can't be deleted. – pedromendessk Jan 28 '19 at 16:02
  • 1
    @RageAgainstTheMachine yes it is definitely from tables with multiple cross dependencies. I want to inform users to run it multiple times, errors are not problems. Run first command 3-4 times then second command 1 time and BOM. It works like charm! – Alper Jan 30 '19 at 05:26
  • @RageAgainstTheMachine , can you confirm that this sp will only affect the tables in the database that your query window is currently connected to? I just want to make absolutely sure that this will only affect the database I'm currently in, rather than all of the databases in the server. – Kyle Vassella Jun 16 '20 at 22:18
  • 2
    @KyleVassella yes, this will execute only on the database in which you have your console open – pedromendessk Jun 17 '20 at 11:44
61

If you don't want to type, you can create the statements with this:

USE Databasename

SELECT  'DROP TABLE [' + name + '];'
FROM    sys.tables

Then copy and paste into a new SSMS window to run it.

Dave.Gugg
  • 6,561
  • 3
  • 24
  • 43
46

The simplest way is to drop the whole database and create it once again:

drop database db_name
create database db_name

That's all.

As pointed out in the comments, this does have some important side-effects to be aware of (depending on your needs, these effects may or may not be desirable):

  • This will wipe off all the other system objects like Procedures, views etc., associated the table.
  • You'll lose any users you added to the database.
Scotty Jamison
  • 10,498
  • 2
  • 24
  • 30
Supervision
  • 1,683
  • 1
  • 18
  • 23
30

You could also use the following script to drop everything, including the following:

  • non-system stored procedures
  • views
  • functions
  • foreign key constraints
  • primary key constraints
  • system_versioning
  • tables

https://michaelreichenbach.de/posts/how-to-drop-everything-in-a-mssql-database/

/* Drop all non-system stored procs */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])

WHILE @name is not null
BEGIN
    SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Procedure: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all views */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped View: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all functions */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Function: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all Foreign Key constraints */
DECLARE @name VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

WHILE @name is not null
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint IS NOT NULL
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']'
        EXEC (@SQL)
        PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)
END
GO

/* Drop all Primary Key constraints */
DECLARE @name VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

WHILE @name IS NOT NULL
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint is not null
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']'
        EXEC (@SQL)
        PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)
END
GO

/* Remove system versioning */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    IF OBJECTPROPERTY(OBJECT_ID(' + @name + '), 'TableTemporalType') = 2 
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] SET (SYSTEM_VERSIONING = OFF); ALTER TABLE [dbo].[' + RTRIM(@name) + '] DROP PERIOD FOR SYSTEM_TIME;'
        EXEC (@SQL)
        PRINT 'System Versioning Disabled for Table: ' + @name
        SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all tables */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Table: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO
Silthus
  • 1,679
  • 1
  • 23
  • 27
24

As a follow-up to Dave.Gugg's answer, this would be the code someone would need to get all the DROP TABLE lines in MySQL:

SELECT CONCAT('DROP TABLE ', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_SCHEMA = 'your_database_name'
OMA
  • 3,442
  • 2
  • 32
  • 39
  • 6
    I know the original poster labeled the question with "sql-server", but this might be useful to someone looking to do this in MySQL. In fact I found this question when googling for a MySQL solution to this question, so I'm now sharing the solution I came up with after reading one of the answers here. – OMA Nov 12 '15 at 22:11
  • This worked like a magic which I totally needed. Thanks a lot – Charith Jayasanka Sep 15 '22 at 11:19
  • Used it on a H2 Database - Works like a charm. Thx. – snukone Mar 07 '23 at 16:05
9

I'd just make a small change to @NoDisplayName's answer and use QUOTENAME() on the TABLE_NAME column and also include the TABLE_SCHEMA column encase the tables aren't in the dbo schema.

DECLARE @sql nvarchar(max) = '';

SELECT @sql += 'DROP TABLE ' + QUOTENAME([TABLE_SCHEMA]) + '.' + QUOTENAME([TABLE_NAME]) + ';'
FROM [INFORMATION_SCHEMA].[TABLES]
WHERE [TABLE_TYPE] = 'BASE TABLE';

EXEC SP_EXECUTESQL @sql;

Or using sys schema views (as per @swasheck's comment):

DECLARE @sql nvarchar(max) = '';

SELECT @sql += 'DROP TABLE ' + QUOTENAME([S].[name]) + '.' + QUOTENAME([T].[name]) + ';'
FROM [sys].[tables] AS [T]
INNER JOIN [sys].[schemas] AS [S] ON ([T].[schema_id] = [S].[schema_id])
WHERE [T].[type] = 'U' AND [T].[is_ms_shipped] = 0;

EXEC SP_EXECUTESQL @sql;
AeroX
  • 3,387
  • 2
  • 25
  • 39
  • 4
    i'd generally stick with the `[sys]` schema views if portability across rdbms' is not required. stackoverflow.com/a/3654313/251174 – swasheck Dec 22 '14 at 16:51
  • 1
    @swasheck Thanks for the link that was quite interesting. I've updated the answer with a solution using sys schema views. – AeroX Dec 22 '14 at 16:59
  • You can use Schema_name() function to get the schema name instead of joining http://msdn.microsoft.com/en-us/library/ms175068.aspx – Pரதீப் Dec 22 '14 at 17:00
  • @NoDisplayName Just because you *can*, doesn't mean that you *should*... http://blogs.sqlsentry.com/aaronbertrand/bad-habits-metadata-helper-functions/ – Aaron Bertrand Dec 22 '14 at 17:02
  • @AaronBertrand - My bad thought it could be a better way. thanks for pointing it out. – Pரதீப் Dec 22 '14 at 17:09
  • @NoDisplayName It's a little less typing but not worth it IMHO. Especially if you have to reference it multiple times. – Aaron Bertrand Dec 22 '14 at 17:10
6

If anybody else had a problem with best answer's solution (including disabling foreign keys), here is another solution from me:

-- CLEAN DB
USE [DB_NAME]
    EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
    EXEC sp_MSForEachTable 'DELETE FROM ?'

    DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR
    SET @Cursor = CURSOR FAST_FORWARD FOR

    SELECT DISTINCT sql = 'ALTER TABLE [' + tc2.TABLE_NAME + '] DROP [' + rc1.CONSTRAINT_NAME + ']'
    FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1
    LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME
    OPEN @Cursor FETCH NEXT FROM @Cursor INTO @Sql
    WHILE (@@FETCH_STATUS = 0)
      BEGIN
        Exec SP_EXECUTESQL @Sql
        FETCH NEXT 
        FROM @Cursor INTO @Sql
      END
    CLOSE @Cursor DEALLOCATE @Cursor
    GO

    EXEC sp_MSForEachTable 'DROP TABLE ?'
    GO

    EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL'
Amirhossein
  • 1,148
  • 3
  • 15
  • 34
Ani
  • 2,636
  • 1
  • 21
  • 17
4

Not quite 1 query, still quite short and sweet:

-- Disable all referential integrity constraints
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

-- Drop all PKs and FKs
declare @sql nvarchar(max)
SELECT @sql = STUFF((SELECT '; ' + 'ALTER TABLE ' + Table_Name  +'  drop constraint ' + Constraint_Name  from Information_Schema.CONSTRAINT_TABLE_USAGE ORDER BY Constraint_Name FOR XML PATH('')),1,1,'')
EXECUTE (@sql)
GO

-- Drop all tables
EXEC sp_msforeachtable 'DROP TABLE ?'
GO
xx1xx
  • 1,834
  • 17
  • 16
4

Use the following script to drop all constraints:

DECLARE @sql NVARCHAR(max)=''

SELECT @sql += ' ALTER TABLE ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) +    ' NOCHECK CONSTRAINT all; '
FROM   INFORMATION_SCHEMA.TABLES
WHERE  TABLE_TYPE = 'BASE TABLE'

Exec Sp_executesql @sql

Then run the following to drop all tables:

select @sql='';

SELECT @sql += ' Drop table ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) + '; '
FROM   INFORMATION_SCHEMA.TABLES
WHERE  TABLE_TYPE = 'BASE TABLE'

Exec Sp_executesql @sql

This worked for me in Azure SQL Database where 'sp_msforeachtable' was not available!

Chanukya
  • 5,833
  • 1
  • 22
  • 36
3

Try this

DROP DATABASE database_name;
CREATE DATABASE database_name;

This will delete the database and create a new database with the same name

Merrin K
  • 1,602
  • 1
  • 16
  • 27
1

I Know this question is very old but every Time i need this code .. by the way if you have tables and views and Functions and PROCEDURES you can delete it all by this Script .. so why i post this Script ?? because if u delete all tables you will need to delete all views and if you have Functions and PROCEDURES you need to delete it too
i Hope it will help someone

DECLARE @sql NVARCHAR(max)=''

 SELECT @sql += ' Drop table ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) 
+ '; '
FROM   INFORMATION_SCHEMA.TABLES
WHERE  TABLE_TYPE = 'BASE TABLE'

Exec Sp_executesql @sql


 DECLARE @sql VARCHAR(MAX) = ''
    , @crlf VARCHAR(2) = CHAR(13) + CHAR(10) ;

 SELECT @sql = @sql + 'DROP VIEW ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + 
 QUOTENAME(v.name) +';' + @crlf
 FROM   sys.views v

 PRINT @sql;
 EXEC(@sql);

 declare @procName varchar(500)
 declare cur cursor 

 for select [name] from sys.objects where type = 'p'
 open cur
 fetch next from cur into @procName
 while @@fetch_status = 0
 begin
  exec('drop procedure [' + @procName + ']')
fetch next from cur into @procName
 end
  close cur
  deallocate cur

  Declare @sql NVARCHAR(MAX) = N'';

    SELECT @sql = @sql + N' DROP FUNCTION ' 
               + QUOTENAME(SCHEMA_NAME(schema_id)) 
               + N'.' + QUOTENAME(name)
    FROM sys.objects
  WHERE type_desc LIKE '%FUNCTION%';

   Exec sp_executesql @sql
  GO
Aladein
  • 184
  • 2
  • 13
1

Simply deleting all tables will not work. Since one table depends on another, you must first remove all child tables, and then only remove the root ones.

dbForge Studio has the functionality of generating a drop table script, and in the correct sequence, focusing on dependencies. To do this, select all tables in the database browser folder, then invoke the command by selecting it from the context menu:

Generate Scripts -> Drop -> To new SQL Document
0

I‘d do it with a loop for all tables if you’re using like oracle or sqlite:

FOR i IN (SELECT ut.table_name
              FROM USER_TABLES ut) LOOP
    EXECUTE IMMEDIATE 'drop table '|| i.table_name ||' CASCADE CONSTRAINTS ';
  END LOOP;
Inquisitus
  • 55
  • 3
0

This solution worked for me in Azure SQL on a database with tables in multiple schema's, including system versioned tables.

It uses a cursor to go through all constraints and tables, so it won't end up in an endless loop if you would get an error somewhere.

--drop all FK's
DECLARE @tableName VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)
DECLARE @SchemaName varchar(254)

DECLARE cur CURSOR FOR SELECT c.TABLE_NAME,c.CONSTRAINT_SCHEMA, c.CONSTRAINT_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS c WHERE c.CONSTRAINT_CATALOG=DB_NAME() AND c.CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME 
OPEN cur

FETCH NEXT FROM cur INTO @tableName, @SchemaName, @constraint

WHILE @@FETCH_STATUS = 0 BEGIN
    SELECT @SQL = 'ALTER TABLE [' + RTRIM(@SchemaName) + '].[' + RTRIM(@tableName) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']'
    EXEC (@SQL)
    PRINT 'dropped constraint : ' + @constraint
    FETCH NEXT FROM cur INTO @tableName, @SchemaName, @constraint
END
CLOSE cur    
DEALLOCATE cur
GO

--Set system versioning off

DECLARE @tableName VARCHAR(128)
DECLARE @SQL VARCHAR(254)
DECLARE @SchemaName varchar(254)

DECLARE cur CURSOR FOR select  t.name as TableName, schema_name(t.schema_id) as SchemaName from sys.tables t where t.temporal_type = 2 
OPEN cur

FETCH NEXT FROM cur INTO @tableName, @SchemaName

WHILE @@FETCH_STATUS = 0 BEGIN
    SELECT @SQL = 'ALTER TABLE [' + RTRIM(@SchemaName) + '].[' + RTRIM(@tableName) +'] SET (SYSTEM_VERSIONING = OFF);'
    EXEC (@SQL)
    PRINT 'Removed versioning from: ' + @tableName
    FETCH NEXT FROM cur INTO @tableName, @SchemaName
END
CLOSE cur    
DEALLOCATE cur
GO

--Drop all tables

DECLARE @tableName VARCHAR(128)
DECLARE @SQL VARCHAR(254)
DECLARE @SchemaName varchar(254)

DECLARE cur CURSOR FOR SELECT t.TABLE_NAME, t.TABLE_SCHEMA  from INFORMATION_SCHEMA.TABLES t where t.TABLE_TYPE = 'BASE TABLE' 
OPEN cur

FETCH NEXT FROM cur INTO @tableName, @SchemaName

WHILE @@FETCH_STATUS = 0 BEGIN
    SELECT @SQL = 'DROP TABLE [' + RTRIM(@SchemaName) + '].[' + RTRIM(@tableName) +']'
    EXEC (@SQL)
    PRINT 'Dropped Table: ' + @tableName
    FETCH NEXT FROM cur INTO @tableName, @SchemaName
END
CLOSE cur    
DEALLOCATE cur
GO
Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
-1

For me I just do


DECLARE @cnt INT = 0;

WHILE @cnt < 10 --Change this if all tables are not dropped with one run
BEGIN
SET @cnt = @cnt + 1;
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
END

Michael
  • 395
  • 1
  • 13