Long time blog reader and SQL Server Expert Simon Worth has suggested two additional method to achieve same results as described in article SQL SERVER – Find Table in Every Database of SQL Server. Both of his methods rely on sp_msforeachdb.

Method 1
sp_msforeachdb "SELECT '?' DatabaseName, Name FROM ?.sys.Tables WHERE Name LIKE '%address%'"
Method 2
CREATE TABLE #TableNameResults (DatabaseName VARCHAR(100) NOT NULL, TableName VARCHAR(100) NOT NULL)
INSERT INTO #TableNameResults
EXEC sp_msforeachdb "SELECT '?' DatabaseName, Name FROM ?.sys.Tables WHERE Name LIKE '%address%'"
SELECT *
FROM #TableNameResults
DROP TABLE #TableNameResults
What to Watch When You Find Table in Every Database
Both methods are short and easy to remember, which is why I like them for a quick search. Keep in mind that sp_msforeachdb is undocumented. Microsoft does not support it, and it can change or behave differently between versions. For a one time search on your own server that is fine. For a job that runs every night, I prefer a loop I control.
A few tips that make these scripts more reliable:
- Put brackets around the placeholder, like
[?].sys.tables. A database name with a space or a dash breaks the query otherwise. - Offline or restoring databases cause errors. If you build your own loop, read
sys.databasesand keep only the rows wherestate_desc = 'ONLINE'. - Size the temp table columns as
sysnameinstead of VARCHAR(100), because object names can be up to 128 characters long. - The LIKE pattern
'%address%'matches any table with that word anywhere in its name. Use an exact name with = when you know it.
If you write your own loop, build the query with QUOTENAME(name) for each database, run it with sp_executesql, and insert the results into one temp table. It takes a few more lines, but you know exactly which databases were searched.
The same pattern works for more than tables. Point it at sys.columns to find every database with a column of a given name, or at sys.procedures to find where a stored procedure lives. You only change the view in the query and the name in the LIKE.
One more note: the search can only read databases your login can access. If a database is missing from the results or shows an error, check your rights on it before you decide the table does not exist. A quick test on a database you know well confirms the script before you trust it on all of them.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.
Discover more from SQL Authority with Pinal Dave
Subscribe to get the latest posts sent to your email.





8 Comments. Leave new
Good tips. Keep up the good work.
Thanks.
Hi, i noticed an error when running the above on sql 2005:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ‘.1’.
(1 row(s) affected)
(1 row(s) affected)
Any idea what could be going wrong?
Thanks & Regards,
Vinod Joseph
Hi to all,
i am developer for the past two year,
i want to gain knowledge for next level. i don’t have time to go for an institue also here, no such institute will teach sql server perfectly.
Can any one suggest basic to advance sql learning videos?
download?
please friends.
Hey Vinod,
The error you are getting is being cause by the single quote being a `. Make sure all sure that all the `s are single quotes.
As ms_foreachdb is undocumented, I prefer doing
the ” incorrect syntax near 1 ” could also be caused by a database name that begins with the number 1.
Thanks a lot sir.
your welcome Channdeep.