Solved

Drop all Tables

Posted on 2013-12-17
10
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 60

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 60

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 60

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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