Solved

Drop all Tables

Posted on 2013-12-17
10
498 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

713 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