Pages

Tuesday, February 8, 2011

Query to find particular object belonging to what database in SQL Server

Query to find particular object belonging to what database in SQL Server

 
Suppose if your SQL Server is having more than 100 databases and you got a challenge to find a particular object (proc/function/table/view..etc) is in which database?

Either we have to login into each db and check the sysobjects
or we can run a single query in master db which can query all the DB's, search what you want and bring that you...later one is the better option coz it is time saving..:)

Use below query to find the result
Exec sp_MSforeachdb 'if EXISTS (select * from ?..sysobjects where name like ''DummyTable1'')
Begin
Select name from sysdatabases where name like ''?''
END'
---You just need to replace "DummyTable1" with your required object name and you can very well add more conditions to that query...