Solved

How to get Database name for a given table name

Posted on 2009-05-07
3
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Expert Comment

by:JestersGrind
ID: 24324719
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
ID: 24324865
Actually, it is not returning any db name.
0
 
LVL 21

Accepted Solution

by:
JestersGrind earned 50 total points
ID: 24324914
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

623 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