Solved

list many tables so I can delete them

Posted on 2011-09-03
13
286 Views
Last Modified: 2012-05-12
select * tables from database


I want to drop alot of tables so
I want the output to look like this

drop tables table1,table2,table3,...


or a way I can easy copy paste
0
Comment
Question by:rgb192
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 5

Expert Comment

by:Brian Chan
ID: 36478240
I don't think you can drop multiple tables in a single DROP statement. I ususlly use excel spreadsheet to concatenate the table name with the "DROP TABLE", or with my favourite text editor, Notepad++, which has the feature to insert like column.
0
 
LVL 14

Expert Comment

by:leoahmad
ID: 36478248
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_27289072.html

As you have already been suggested that you cannot do that in single statement, why not use the script given in the above link and get statements of deletion.

By the way, why you want a single statement?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36478273
Why are you reposting the same question?

Here is my answer from your last duplicate question:
You should be able to do:
DROP TABLE table1, table2, table3

>>is there a right click shortcut<<
From Object Explorer Details in SSMS you can select them all and then delete them all at once.
0
 

Author Comment

by:rgb192
ID: 36478316
I need a select * tables from database

or I would have to write 50 databases by hand



eventually I want to delete them


but most important, I need to know the tablenames
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36478340
So instead of  just deleting them all from SSMS with one delete, you want a SQL statement to concatenate all your table names and build your DROP TABLE to look like this:
DROP TABLE table1, table2, table3, ...
and then execute it.  Is that correct?

0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 250 total points
ID: 36478354
select 'Drop table ['+table_schema+'].['+table_name+']'
from information_schema.tables
where table_type='Base Table' -- to ignore views
 and table_name like ',,,,%'
order by 1

you may also wish to investigate the procedure sp_msforeachtable

which allows you to construct sql commands to process against a set of tables...
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:rgb192
ID: 36479031
>>So instead of  just deleting them all from SSMS with one delete, you want a SQL statement to concatenate all your table names and build your DROP TABLE to look like this:
DROP TABLE table1, table2, table3, ...
and then execute it.  Is that correct?


correct



but first is there a way I can select the names of my tables so I dont have to write by hand
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36479325
Did you see Lowfatspread's solution?
0
 

Author Comment

by:rgb192
ID: 36519235
>>
select 'Drop table ['+table_schema+'].['+table_name+']'
from information_schema.tables
where table_type='Base Table' -- to ignore views
 and table_name like ',,,,%'
order by 1

is there a sample command so I dont destroy all my tables







>>
you may also wish to investigate the procedure sp_msforeachtable

I do not understand

>>
which allows you to construct sql commands to process against a set of tables...

I do not understand
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36519285
>>select 'Drop table ['+table_schema+'].['+table_name+']'
from information_schema.tables
where table_type='Base Table' -- to ignore views
 and table_name like ',,,,%'
order by 1


just generates the command ... it doesnt actually run the drop...


 >>procedure sp_msforeachtable
http://www.databasejournal.com/features/mssql/article.php/3441031/SQL-Server-Undocumented-Stored-Procedures-spMSforeachtable-and-spMSforeachdb.htm

0
 

Author Comment

by:rgb192
ID: 36555100
select 'Drop table ['+table_schema+'].['+table_name+']'
from information_schema.tables
where table_type='Base Table' -- to ignore views
 and table_name like ',,,,%'
order by 1



returns a column named (No column name)
and no results
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 36555270
You are obviously not grasping the intent so let's try it this way (No points please as this is Lowfatspread's solution):
SELECT  'Drop table [' + table_schema + '].[' + table_name + ']'
FROM    information_schema.tables
0
 

Author Closing Comment

by:rgb192
ID: 36556834
thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now