Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

list many tables so I can delete them

Posted on 2011-09-03
13
Medium Priority
?
362 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:Muhammad Ahmad Imran
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1000 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 1000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

810 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