Click here to Skip to main content
13,195,564 members (70,608 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

41.4K views
3 bookmarked
Posted 31 Jan 2012

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)

Share

About the Author

Gaushick
Software Developer
India India
.Net Programmer

You may also be interested in...

Comments and Discussions

 
QuestionSQL Server 2008 Tried the following Pin
Timotheos10-Nov-12 1:16
memberTimotheos10-Nov-12 1:16 
GeneralFirst and foremost, thanks for taking the time to share. A f... Pin
Pablo Aliskevicius31-Jan-12 20:01
memberPablo Aliskevicius31-Jan-12 20:01 
SuggestionConstraints Pin
yannduran31-Jan-12 14:03
memberyannduran31-Jan-12 14:03 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171019.1 | Last Updated 31 Jan 2012
Article Copyright 2012 by Gaushick
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid