Solved

How to get Database name for a given table name

Posted on 2009-05-07
3
382 Views
Last Modified: 2012-05-06
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
Comment
Question by:praveenkumar_3i
  • 2
3 Comments
 
LVL 21

Expert Comment

by:JestersGrind
Comment Utility
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
 

Author Comment

by:praveenkumar_3i
Comment Utility
Actually, it is not returning any db name.
0
 
LVL 21

Accepted Solution

by:
JestersGrind earned 50 total points
Comment Utility
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now