Solved

Drop all Tables

Posted on 2013-12-17
10
490 Views
Last Modified: 2014-03-03
I want to be able to drop all tables in a database.

I've found this.

USE [database]
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"

Just need to check that t will not delete any system tables ect.
0
Comment
Question by:aneilg
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 102 total points
ID: 39723966
If I remember correctly, sp_MSforeachtable excludes system tables; however, it always works best to test first.  I usually use PRINT command, or something I can audit.  In addition, you can add a filter with the @whereand parameter.  Hence, you can make sure only the tables you want match the filter.  For example, only user tables will get printed with this code.

sp_MSForEachTable
    @command1 = "PRINT '?'",
    @whereand = "AND uid = ANY (SELECT uid FROM sys.objects WHERE [type] = 'U')"
;

Open in new window

0
 
LVL 17

Assisted Solution

by:dbaSQL
dbaSQL earned 101 total points
ID: 39726796
I like mwvisa1's suggestion, because sp_MSForEachTable is very quick and easy, but it is also an undocumented proc. While there are many common uses for the undocumented procs, we should try to avoid them, if we can.

Here's another method that should work to remove your tables:

--DROP ALL TABLES
USE YourDatabaseName
GO
DECLARE @TableName VARCHAR(150)
DECLARE @SQL VARCHAR(300)
 
SELECT @TableName = (
      SELECT TOP 1 [name]
      FROM sys.objects
      WHERE [type] = 'U'
      AND [name] LIKE N'TableName%'
      ORDER BY [name] )
 
WHILE @TableName IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@TableName) +']'
    EXEC (@SQL)
    PRINT 'Dropped Table : ' + @TableName
    SELECT @TableName = (
      SELECT TOP 1 [name]
      FROM sys.objects
      WHERE [type] = 'U'
      AND [name] LIKE N'TableName%'  
      AND [name] > @TableName
      ORDER BY [name] )
END
0
 

Author Comment

by:aneilg
ID: 39726881
Thanks I'll have a look at both.
0
 

Author Comment

by:aneilg
ID: 39728800
dbaSQL not sure what your code is doing but it does not delete any table from my database.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39728990
aneilg, my code does not DROP anything because I wanted you to use PRINT to verify the filter was working the way you wanted first.  Once you are satisfied, change the code back to the DROP statement you had.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:aneilg
ID: 39729036
OK, Thanks.
You were right in being cautious with EXEC sp_MSforeachtable it drops the system tables unless specified.

Once again thanks.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39729055
You are most welcome!

Best regards and happy coding,

Kevin
0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 39729112
my apologies, aneilg.  I will take a look and see where the problem is.
0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 39729808
Corrected code is below.  It was just that I had left this tester line in there:

       AND [name] LIKE N'TableName%'

Had I used the PRINT command first, as mwvisa1 suggested, we would have seen that in the output.  Always very good to PRINT before EXEC.



--DROP ALL TABLES
USE YourDatabase
GO

DECLARE @TableName VARCHAR(150)
DECLARE @SQL VARCHAR(300)
 
SELECT @TableName = (
      SELECT TOP 1 [name]
      FROM sys.objects
      WHERE [type] = 'U'
      ORDER BY [name] )
 
WHILE @TableName IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@TableName) +']'
  --  PRINT @SQL
      EXEC (@SQL)
    PRINT 'Dropped Table : ' + @TableName
    SELECT @TableName = (
      SELECT TOP 1 [name]
      FROM sys.objects
      WHERE [type] = 'U'
      AND [name] > @TableName
      ORDER BY [name] )
END
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 102 total points
ID: 39733735
Instead of using sys.objects it is a lot easier to use sys.tables.  From SQL Server's BOL:
Returns a row for each table object, currently only with sys.objects.type = U.
It is also a good idea to include the schema in any query.  It may not always be dbo or you may not have permission to drop tables that are in the dbo schema.

And of course it should be pointed out that all these simple strategies rely on the fact that the table has no foreign key relationships.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now