Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 drop all obects with same name

Author  Topic 

clhereistian
Starting Member

2 Posts

Posted - 2008-05-21 : 15:54:49
I need to use T-SQL to drop all instances of a particular object, no matter who the owner is. For example I might have two instances of a view named "test_view". One is owned by "dbo" and one is owned by "randomly_named_user". I do not know what the name of the owner of the second instance of the view, nor do I know how man instances of the view may exist. I need to drop all the views with the name "test_view".

Is there a way with T-SQL to drop all the instances of an object no matter who owns it? I understand I will need proper permissions to drop an object owned by someone else.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-21 : 16:04:35
This will output the queries that you will need to run, make sure you run it in the database where the objects exist.

SELECT 'DROP TABLE ' + TABLE_SCHEMA + '.' + TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'PutViewNameHere'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

clhereistian
Starting Member

2 Posts

Posted - 2008-05-21 : 16:15:11
Awesome! That works great.
Go to Top of Page
   

- Advertisement -