snippetsqlModerate
How can I find a table in the whole instance?
Viewed 0 times
canthehowwholeinstancefindtable
Problem
I am trying to create a report which selects data from a table called
How can I search for a table in the whole instance?
costomersHR. I cannot find it as we have hundreds of databases in the instance.How can I search for a table in the whole instance?
Solution
To do it through code...
1) If you can assume that this table is in the
2) Otherwise you could loop through all databases (and query
1) If you can assume that this table is in the
dbo schema.SELECT name
FROM sys.databases
WHERE CASE
WHEN state_desc = 'ONLINE'
THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[costomersHR]', 'U')
END IS NOT NULL2) Otherwise you could loop through all databases (and query
sys.tables in each) with something like this code for a more reliable and flexible sp_MSforeachdbCode Snippets
SELECT name
FROM sys.databases
WHERE CASE
WHEN state_desc = 'ONLINE'
THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[costomersHR]', 'U')
END IS NOT NULLContext
StackExchange Database Administrators Q#124835, answer score: 17
Revisions (0)
No revisions yet.