• subscribe
April 09, 2009 12:00 AM

Need to Find an Object But You Don't Know Its Exact Name? Try this Search Engine

SQL Server Pro
InstantDoc ID #101693
Downloads
101693.zip

Have you ever tried to look for an object in a SQL Server instance that has hundreds of databases without knowing the object's exact name and the database in which it resides? It can be quite time-consuming, especially if the databases include objects with similar names. To make the search quicker, I created sp_ObjectSearch. This stored procedure checks objects' names for the string you specify. It searches through all the objects in each database within the current SQL instance.

To call the sp_ObjectSearch stored procedure, you use the syntax

sp_ObjectSearch 'search string'

where search string is the target string. For example, suppose you need to find an object whose name includes the word access. You'd use code such as

EXEC master..sp_ObjectSearch 'access'

Figure 1 shows sample results from this query.


Figure 1: Sample results from the sp_ObjectSearch store procedure



As you can see, six databases contain objects whose name includes the string access. Besides specifying the database's name and the object's name, the result set specifies the type of object. The sp_ObjectSearch store procedure handles many types of objects, including user-defined tables, SQL stored procedures, views, and primary key and foreign key constraints. As Listing 1 shows, it uses a simple CASE function to identify each object's type. (For information about both simple and searched CASE functions, see "T-SQL 101: The CASE Function".)


Listing 1: Case Function that Identifies the Type of Object



The sp_ObjectSearch stored procedure works on SQL Server 2005 and later. For backward compatibility, I created sp_ObjectSearch_2K. You can download both stored procedures by clicking the 101693.zip hotlink at the top of the page.



ARTICLE TOOLS

Comments
  • Ryan
    3 years ago
    Jun 11, 2009

    This seems like a fancy way to retrieve catalog data. The INFORMATION_SCHEMA views can provide you with the same, or more, information.

    For example, if you were looking for all tables containing "access" in a database, you can try:

    select table_catalog as DBName, table_name as TableName, table_type as TableType from information_schema.tables where table_name like '%access%'

    If you needed to apply that query to all databases you can run it with sp_msforeachdb like this:

    exec sp_msforeachdb 'use ?; select table_catalog as dbname, table_name as tablename, table_type as TableType from information_schema.tables where table_name like '%access%''

    With only slight modifications this can be applied to the other Information Schema Views for the type of object you are looking for.

    http://msdn.microsoft.com/en-us/library/ms186778.aspx

    rbs

  • Marcos
    3 years ago
    May 20, 2009

    Nice script to have.

  • TRACEY
    3 years ago
    May 19, 2009

    If you have a table CUSTOMER and have column CUSTOMER_ID you cannot search for the CUSTOMER_ID - column within TABLE.

  • YONI
    3 years ago
    May 02, 2009

    for quick searches, there's a free utility out there that does this, with a nice GUI too:
    http://www.nobhillsoft.com/DianaLite.aspx

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...