Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Drop all Tables

Posted on 2013-12-17
10
Medium Priority
?
524 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 408 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 404 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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 

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 408 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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