March 3, 2008 at 3:03 am
Hi,
I have several databases in my server.Each database contain several tables.I want to write a query for the following :
For eg: If I have a table named "Customer" I want to know in which database the particular table exists.
Could anyone help me
Thanks in advance..
March 3, 2008 at 6:40 am
You'll have to walk through the databases on the server. Try something like this. It'll work, but it's not perfect.
sp_msforeachdb 'select DB_NAME() AS ''DBName''
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_NAME = ''Customer'''
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
March 3, 2008 at 6:41 am
You should use the foreachdatabase.
EXEC master..sp_MSForeachdb '
USE [?]
IF DB_ID(''?'')>4
BEGIN
IF EXISTS(SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = ''Customer'')
BEGIN
PRINT ''?''
END
END
'
March 3, 2008 at 6:42 am
Same concept but a little different twist.
March 3, 2008 at 6:43 am
Yeah, always more than one way to skin a cat.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply