• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

How to get Database name for a given table name

I need to get all the data base name which has the given table

For Eg.

Select DataBaseName from Sys.databases where tableName = 'Table1'

it should display all that database which has a table 'Table1'
0
praveenkumar_3i
Asked:
praveenkumar_3i
  • 2
1 Solution
 
JestersGrindCommented:
The query below will query each database for the table, Table1.

Greg



EXEC sp_MSforeachdb "SELECT '?' FROM ?.sys.tables WHERE [name] = 'Table1'"

Open in new window

0
 
praveenkumar_3iAuthor Commented:
Actually, it is not returning any db name.
0
 
JestersGrindCommented:
You will get a result set from each database on the server.  If the dataset is empty, the table doesn't exist in that particular database.  

Greg




0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now