SQL SERVER – Find Table in Every Database of SQL Server – Part 2 Extension

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.

SQL SERVER - Find Table in Every Database of SQL Server - Part 2 Extension

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.databases and keep only the rows where state_desc = 'ONLINE'.
  • Size the temp table columns as sysname instead 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.

SQL Scripts, Undocumented SQL
Previous Post
ACOS: Check the Cosine Domain Before Calculating Angles
Next Post
SQL SERVER 2005 – FIX Error: 18456 : VISTA Windows Authentication

Related Posts

8 Comments. Leave new

  • Good tips. Keep up the good work.
    Thanks.

    Reply
  • Vinod Joseph
    May 15, 2008 11:37 am

    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

    Reply
  • Sathish Kumar. K
    May 15, 2008 3:12 pm

    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.

    Reply
  • 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.

    Reply
  • As ms_foreachdb is undocumented, I prefer doing

    Reply
  • SigueSigueSputnik
    December 17, 2012 11:42 pm

    the ” incorrect syntax near 1 ” could also be caused by a database name that begins with the number 1.

    Reply
  • Channdeep Singh
    May 7, 2015 10:40 pm

    Thanks a lot sir.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.