Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get Database name for a given table name

Posted on 2009-05-07
3
Medium Priority
?
393 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 150 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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

721 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