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

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.

Discuss this Article 4

ryanbseattle
on 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
TRACEY (not verified)
on May 19, 2009
If you have a table CUSTOMER and have column CUSTOMER_ID you cannot search for the CUSTOMER_ID - column within TABLE.
yonision
on May 2, 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
MarcosGalvani
on May 20, 2009
Nice script to have.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.