Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Is there a way to search database name using table name. Please help.

Thanks in Advance.

What I have tried:

SELECT name FROM sys.databases WHERE CASE WHEN state_desc = 'ONLINE' THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[TableName]', 'U') END IS NOT NULL

but the above code is not working.
Please help.
Posted
Updated 27-Apr-16 2:11am
Comments
ZurdoDev 27-Apr-16 8:01am    
Do you mean you want to know if a certain table is in a certain db?

1 solution

SQL
SELECT DISTINCT  DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL) 
WHERE OBJECT_NAME(object_id,database_id) = 'Your Table Name'


Ashish
 
Share this answer
 
Comments
Karthik_Mahalingam 28-Apr-16 8:41am    
5
Nigam,Ashish 28-Apr-16 8:45am    
thanks Karthik

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900