Solved

list many tables so I can delete them

Posted on 2011-09-03
13
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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: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
 

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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