Solved

Drop all Tables

Posted on 2013-12-17
10
493 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Anyway to make these 2 SQL statements into one? 13 39
SQL 2008 R2 syntax 11 29
get column names from table in vb.net 8 29
SQL Error - Query 6 26
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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