13

I have a SQL Server with hundreds of databases and each database having hundreds of tables. Now I would like to find where in these databases is a table that I am looking for.

I could find if a table existed in individual database using

use myDatabase 
select * from sys.tables  where name = 'mytable' 
GO

but using this means I have to manually change the database for hundreds of times . I would like to find the database name only. Is there a way out ?

5
  • possible duplicate of sp_MSforeachdb query help Commented Jan 19, 2011 at 8:36
  • 1
    @Damien_The_Unbeliever: The same system stored procedure can be used in the solution but the question is not the same in my opinion. Commented Jan 19, 2011 at 8:48
  • @John Sansom - the title of the other question is poorly worded, but it is dealing with finding and working with a table in each database on a server, where there are other DBs on the server which will not have this table. I thought it was close enough. Commented Jan 19, 2011 at 8:54
  • 1
    @Damien_The_Unbeliever This questions is interested to find the database with a given table , hence i think it is significantly different. Commented Jan 19, 2011 at 9:03
  • sorry, I was obviously reading too much into your question. Usually I find that this is the first part of the longer question "... and having found the databases containing this table, how do I query that table in each db". Commented Jan 19, 2011 at 9:14

7 Answers 7

16

Okay, if you're just wanting to find each database that contains a particular table, and aren't going to be querying the table, then you can just do:

create table #t (
    DBName sysname not null
)
go
exec sp_MSforeachdb 'use [?]; if OBJECT_ID(''dbo.mytable'') is not null insert into #t (DBName) select ''?'''
go
select * from #t
go
drop table #t

(If you're not using multiple schemas in your databases, you won't need to specify dbo in the OBJECT_ID call, otherwise I use it to avoid finding tables in the wrong schema)

Sign up to request clarification or add additional context in comments.

2 Comments

@Martin - TBH, I think I'd just re-purposed another answer which was querying the table in each database.
14

This should do what you are looking for:

EXEC sp_MSforeachdb "use [?];select * from sys.tables where name='TableName' "

To include the name of the current database in the output use:

EXEC sp_MSforeachdb "use [?];select '[?]' as DatabaseName, * from sys.tables where name='TableName' "

2 Comments

The sql executed ,but I could not get the database name from it .
@Thunder so you want the database name to be output in the result set also?
7
SELECT DISTINCT  DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL) 
WHERE OBJECT_NAME(object_id,database_id) = 'mytable'

Comments

3

I know this is an old thread but was high on my google search. So I wanted to contribute for others looking to find a database with a certain table in it. These apply to SQL Server 2008 - Current.

I started with this, which worked for my SA level login, but gave me issues with users that did not have permissions to all databases.

SELECT name
FROM sys.databases
WHERE CASE
WHEN state_desc = 'ONLINE' THEN OBJECT_ID( QUOTENAME( name ) + '.[dbo].[mytablename]','U' )
END IS NOT NULL;

But ended up with this adding the HAS_DBACCESS(name) = 1 in restriction so that the query would not fail with a security error.

SELECT name
FROM sys.databases
WHERE HAS_DBACCESS(name) = 1 and
CASE
WHEN state_desc = 'ONLINE' THEN OBJECT_ID( QUOTENAME( name ) + '.[dbo].[mytablename]','U' )
END IS NOT NULL;

1 Comment

For tables in the default / known schema this is great!
1
exec sp_msforeachdb @command1='     
USE ?;
select * from sys.tables where name = ''CLIENTS'''    

Comments

0

this is also one of the way, similar with solution of @Jonathan :

exec sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%YourTableName%''' 

Comments

-1
exec 'select ''?'', name from [?].sys.tables where name = ''yourTable'''

1 Comment

i guess you want to write this sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%yourtable ame%'''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.