Find Strings in Database Code, SQL Server Agent Jobs, and SSIS Packages

T-SQL stored procedure is functional and flexible

Downloads
101896.zip

After reading Michael Berry's article "Find Any String in Any SQL Code in Any Database", I was inspired to try his Find stored procedure, which lets you find a string of data in any T-SQL code stored in any database on the given SQL Server instance. To call this stored procedure, you type

Find 'mystring'

where mystring is the string you want to search for. The stored procedure then searches for that string in all the databases on the server and returns a list of the objects that contain it.

After sharing the Find stored procedure with my coworkers, we decided to tweak it. In our jobs, we often need to search for strings in not only database code but also SQL Server Agent jobs and SQL Server Integration Services (SSIS) packages, so we adapted the stored procedure, which we renamed sp_Find, to perform these additional searches. The sp_Find stored procedure searches SQL Server Agent jobs by looking through the text stored in all job steps and searches SSIS packages saved to msdb. (It won't search through SSIS packages if they're saved to a file system or through DTS packages.) You can perform one type of search (e.g., only SSIS packages), all three types of searches (i.e., database code, SQL Server Agent jobs, and SSIS packages), or any combination thereof (e.g., database code and SQL Server Agent jobs).

We also adapted the stored procedure so that the output includes 100 characters of the code surrounding the search string (i.e., the 50 characters preceding the string and the 50 characters following the string). You can change the number of characters if desired. Finally, we adapted the stored procedure so that you can search a specific database rather than all the user databases on the server.

To run sp_Find, you follow the syntax

sp_Find 'SearchText', DBName, PreviewTextSize, SearchDBsFlag, SearchJobsFlag, SearchSSISFlag

where

  • SearchText is the string you want to search for
  • DBName is the specific database you want to search. When you don't include this parameter, all the user databases on the server are searched.
  • PreviewTextSize is how many characters of the code surrounding the search string you want included in the output. When you don't include this parameter, the stored parameter includes 100 characters surrounding the search string.
  • SearchDBsFlag specifies whether you want to search databases (Y) or not (N).
  • SearchJobsFlag specifies whether you want to search SQL Server Agent jobs (Y) or not (N).
  • SearchSSISFlag specifies whether you want to search SISS packages (Y) or not (N).

So, for example, the code

sp_Find 'track'

tells the stored procedure to search through all the databases, SQL Server Agent jobs, and SSIS packages for the string track. The statement

sp_Find 'track', 'Common', 50

tells the stored procedure to search through the database named Common, the SQL Server Agent jobs, and the SSIS packages for the string track and include only 50 characters surrounding the string in the output. Figure 1 shows sample results from this statement.


Figure 1: Sample results from the sp_Find stored procedure (click to enlarge)

The code sp_Find 'Track', 'Common', Null, 'Y', 'N', 'Y'

tells the stored procedure to search through the database named Common and the SSIS packages for the string track and include 100 characters surrounding the string in the output. As this example shows, you need to use Null if there's a parameter you don't want to set sandwiched between two parameters that you are setting.

Because our shop is completely on SQL Server 2005, we changed the stored procedure's queries to reflect the latest system catalog. Thus, sp_Find won't work on earlier SQL Server versions. If you prefer to have a search tool that works on earlier versions, it wouldn't be difficult to remove the SSIS package logic and revert the database tables back to the previous catalog version, using the code provided in "Find Any String in Any SQL Code in Any Database" as an example.

You can download the sp_Find.sql file by clicking the 101896.zip hotlink at the top of the page. You don't need to customize the code at all, provided you're going to run it on SQL Server 2008 or SQL Server 2005.

Note that we named this stored procedure sp_Find so that we could store it in the Master system database and call it from any database context. Use this name at your own risk—Microsoft could come out with a system procedure with the same name in a future release.

Learn more from "T-SQL 101: Stored Procedures" and "Relevant T-SQL Stored Procedures."

Discuss this Article 8

gdave
on Oct 12, 2010
I have just downloaded sp_find and applied to MSS2008.I still get the same error that was reported earlier.

msg 208, level 16, state 1, Procedure sp_Find, line 144 Invalid object name 'MSDB.dbo.sysDTSPackages90'

And David's code reference was already in file sp_FindMag.sql

Please help.
Brian (not verified)
on Nov 5, 2009
Can't search SQL 2008 SSIS packages...get this error: Invalid object name 'msdb.dbo.sysDTSPackages90'.
KBemowski
on Jul 22, 2009
I checked with Bill and you're correct. Bill sent along a corrected version of sp_FindMag.sql, which is now in the 101896.zip file. Thanks for letting us know about this! Karen Bemowski, senior editor SQL Server Magazine, Windows IT Pro
drschank
on Apr 4, 2011
Where is the 101896.zip hotlink that is mentioned? I don't see any such link.
David (not verified)
on Jul 16, 2009
The select from #FoundObject is missing the PreviewTest. Select should read: Select DatabaseName , ObjectName , ObjectTypeDesc As ObjectType , PreviewText From #FoundObject Order By DatabaseName, ObjectName;
ocolamatteo
on Feb 10, 2011
Why not post this on codeplex so people can submit bug fixes?
jceddy113
on Nov 27, 2012
Just a comment...when creating a stored procedure for general consumption, you should consider matching the case in your code to the case in the names of the objects used (i.e. sys.sql_modules instead of sys.SQL_Modules), since many companies have their databases set up to be case-sensitive.
blesche
on Apr 26, 2010
Brian, The SQL2008 system tables is MSDB.dbo.sysSSISPackages.

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 Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
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
SQL Server Pro Forums

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