If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > How To Delete All Tables

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-03-04, 02:01
Saravanan.R Saravanan.R is offline
Registered User
 
Join Date: Feb 2004
Location: India
Posts: 135
How To Delete All Tables

Hi All,

How can i delete all the tables in a DATABASE with a single shot!!

Thanx in advance
Reply With Quote
  #2 (permalink)  
Old 04-03-04, 03:56
bennydubai bennydubai is offline
Registered User
 
Join Date: Mar 2004
Location: Dubai
Posts: 29
Re: How To Delete All Tables

Well...what i do is may not be the right way)

Just delete the database.......and create a new database........else it will start asking for so many dependencies)

cheers!







Quote:
Originally posted by Saravanan.R
Hi All,

How can i delete all the tables in a DATABASE with a single shot!!

Thanx in advance
Reply With Quote
  #3 (permalink)  
Old 04-03-04, 04:34
Saravanan.R Saravanan.R is offline
Registered User
 
Join Date: Feb 2004
Location: India
Posts: 135
Benny,

In a database TABLES, VIEWS, FUNCTIONS, PROCEDURES, TRIGGERS, etc. are there! So I have to delete only TABLES. If I drop the database means I want to recreated all those objects.

I need to delete TABLES alone.

Thanx in advance
Reply With Quote
  #4 (permalink)  
Old 04-03-04, 05:38
upalsen upalsen is offline
Registered User
 
Join Date: Feb 2003
Location: India
Posts: 216
another simple way

generate SQL script of the database, select "all tables", select "generate drop command for each object" check box only (not the create one). you should have a script with all drop commands. run it
Reply With Quote
  #5 (permalink)  
Old 04-03-04, 09:39
Saravanan.R Saravanan.R is offline
Registered User
 
Join Date: Feb 2004
Location: India
Posts: 135
Hi

Its not a appropriate way to drop! plz..
Reply With Quote
  #6 (permalink)  
Old 04-03-04, 09:47
TALAT TALAT is offline
Registered User
 
Join Date: Feb 2004
Location: PAKISTAN
Posts: 106
DELETE FROM sysobjects WHERE xtype='U'
Need to check the option "update system catalogs" for the server properties prior to execute the statement.
Reply With Quote
  #7 (permalink)  
Old 04-03-04, 15:25
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
Re: How To Delete All Tables

Quote:
Originally posted by Saravanan.R
Hi All,

How can i delete all the tables in a DATABASE with a single shot!!

Thanx in advance
DO NOT delete anything from sysobjects. That's insane.

If you want to delete the tables (in SQL Server lingo, this just means delete the data out of all of them).

DECLARE
@sql VARCHAR(4000),
@int_counter INT,
@int_max INT

DECLARE @tables TABLE(
ident INT IDENTITY(1,1) PRIMARY KEY,
table VARCHAR(256))

INSERT @tables(table)
SELECT name FROM sysobjects WHERE xtype = 'U'

SELECT
@int_counter = 1,
@int_max = (SELECT MAX(ident) FROM @tables))

WHILE @int_counter <= @int_max
BEGIN

SELECT @sql = 'DELETE ' + table
FROM @tables WHERE ident = @int_counter

SELECT @int_counter = @int_counter + 1
END

This will only work if you don't have foreign keys though, so you would have to make a similar procedure to drop and recreate those. You can find one on SQLServerCentral.com though.

If you really want to just drop all the tables, change the DELETE to DROP TABLE, and you are good to go. NEVER edit the system tables as a shortcut. It's dangerous; and there are too many good scripts someone else has already written to be doing that.
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #8 (permalink)  
Old 04-03-04, 15:35
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,555
"Its not a appropriate way to drop!"???????

What they heck WOULD be an appropriate way to drop all the tables in a database with dependent procedures, views, functions, and perhaps even triggers?

I have trouble believing what you are doing is appropriate or necessary in the first place!
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #9 (permalink)  
Old 04-03-04, 23:52
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
Quote:
Originally posted by blindman
"Its not a appropriate way to drop!"???????

What they heck WOULD be an appropriate way to drop all the tables in a database with dependent procedures, views, functions, and perhaps even triggers?

I have trouble believing what you are doing is appropriate or necessary in the first place!
If he's dropping the tables to just recreate them, the biggest problem will be the foreign keys if he has them.
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #10 (permalink)  
Old 04-04-04, 01:50
TALAT TALAT is offline
Registered User
 
Join Date: Feb 2004
Location: PAKISTAN
Posts: 106
He is just askin to delete ALL tables. Why r u scared of the foreign keys, obviously he would have the script for recreating the tables which would include the relationships too.
Reply With Quote
  #11 (permalink)  
Old 04-04-04, 01:58
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,555
Again, What For???

If he can run a script to restore them, presumably he could run upalsen's script solution to drop them...
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #12 (permalink)  
Old 04-04-04, 03:16
TALAT TALAT is offline
Registered User
 
Join Date: Feb 2004
Location: PAKISTAN
Posts: 106
He says that he wanna delete all tables in one shot, just for fun i guess, therefore one delete query in sysobjects would satiate his desires rather to select all tables and then choosing include drop tables statement and then running the drop table commands for each table.
However, it's clear that playin with system catalogs is not so wise. U r right indeed.
Reply With Quote
  #13 (permalink)  
Old 04-04-04, 13:54
Ruprect Ruprect is offline
12 Monkey Method
 
Join Date: Feb 2004
Location: San Antonio, TX
Posts: 566
Saravanan.R

why do you want to do this
it may be that we may have an alternate solution for your problem other than deleting all of your tables

for example if you want to just remove all of the data from your tables without dropping them, then try the truncate table statement.

[Books Online] Truncate Table

ps if you ever directly modify a system table, we will run you out of town and burn your castle like a group of villagers chasing the frankestein monter
Reply With Quote
  #14 (permalink)  
Old 04-05-04, 01:30
Saravanan.R Saravanan.R is offline
Registered User
 
Join Date: Feb 2004
Location: India
Posts: 135
Hi All,

Sorry I have to DELETE (DROP ALL TABLES) in a single shot!
Reply With Quote
  #15 (permalink)  
Old 04-05-04, 02:02
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,555
You think he wants to drop all the tables in his database "just for fun"?

What the heck are YOUR hobbies?
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On