Click here to Skip to main content
Click here to Skip to main content

Drop all the tables in a database

, 31 Jan 2012
Rate this:
Please Sign up or sign in to vote.
Drop all the tables in a database(give the database name and table name in the below query).
--Drop all the table in the database
USE DATABASE
GO
DECLARE @tname VARCHAR(150)
DECLARE @strsql VARCHAR(300)
 
SELECT @tname = (SELECT TOP 1 [name] FROM sys.objects WHERE [type] = 'U' and [name] like N'TableName%' ORDER BY [name])
 
WHILE @tname IS NOT NULL
BEGIN
    SELECT @strsql = 'DROP TABLE [dbo].[' + RTRIM(@tname) +']'
    EXEC (@strsql)
    PRINT 'Dropped Table : ' + @tname
    SELECT @tname = (SELECT TOP 1 [name] FROM sys.objects WHERE [type] = 'U' AND [name] like N'TableName%'  AND [name] > @tname ORDER BY [name])
END

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gaushick
Software Developer
India India
.Net Programmer

Comments and Discussions

 
QuestionSQL Server 2008 Tried the following PinmemberTimotheos10-Nov-12 1:16 
GeneralFirst and foremost, thanks for taking the time to share. A f... PinmemberPablo Aliskevicius31-Jan-12 20:01 
SuggestionConstraints Pinmemberyannduran31-Jan-12 14:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 31 Jan 2012
Article Copyright 2012 by Gaushick
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid