Solved

How to get Database name for a given table name

Posted on 2009-05-07
3
389 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
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

830 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