Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

list many tables so I can delete them

Posted on 2011-09-03
13
Medium Priority
?
355 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: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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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