Apr 25

Search Stored Procedure Text

There will come a day, if it has not already come and gone, when you will want to search through your stored procedures because you don’t remember which one did what you want to do.  Did that make sense?  Because I don’t know if it did.  Anyway, here is what we have…

  1. You
  2. A bunch of stored procedures
  3. Something you are looking for inside one or more of your stored procedures
  4. A long night ahead opening each one and using CTRL + F

Luckily there are a couple of super easy solutions.  Below are two such solutions.  The first one is real simple, however, it may not produce the correct results on larger procedures.


The problem with this method is that it only stores the first 8000 characters of the stored procedure so you may miss what you are looking for.  Another way to go about searching is by using the following query.

name AS ProcedureName,
FROM sys.procedures

This should get you everything you need without worrying about the procedures that are over 8000 characters long.  There is also another way to query the stored procedures as well as functions.  So I created the little query below that will allow you to search those objects in addition to stored procedures. If you really want to toss a little cursor or while loop around this and iterate through all stored procedures and functions across all databases.

DB_NAME() AS DatabaseName,
CASE WHEN OBJECTPROPERTY(object_id, 'IsProcedure') = 1 THEN 'Stored Procedure'
WHEN OBJECTPROPERTY(object_id, 'IsTableFunction') = 1 THEN 'Table Function'
WHEN OBJECTPROPERTY(object_id, 'IsScalarFunction') = 1 THEN 'Scalar Function'
WHEN OBJECTPROPERTY(object_id, 'IsInlineFunction') = 1 THEN 'Inline Function'
OBJECT_SCHEMA_NAME(object_id) AS ObjectSchema,
OBJECT_NAME(object_id) ObjectName,
definition AS ObjectText
FROM sys.sql_modules
WHERE OBJECTPROPERTY(object_id, 'IsProcedure') = 1
OR OBJECTPROPERTY(object_id, 'IsTableFunction') = 1
OR OBJECTPROPERTY(object_id, 'IsScalarFunction') = 1
OR OBJECTPROPERTY(object_id, 'IsInlineFunction') = 1
AND definition LIKE '%Text%'

Hope you enjoy!


1 ping

Skip to comment form

  1. Kenya Nevarrez

    Beneficial info and excellent design you got here! I want to thank you for sharing your ideas and putting the time into the stuff you publish! Great work!

  2. Nevada Curra

    Thank you, I have recently been searching for information about this topic for ages and yours is the best I have discovered so far.

  3. James M

    Hi Bradley,

    Thank you for another great post! I had this problem in January and really wished I had found your site rather than wasting 30 minutes in figuring out the solution!

    Keep up the good work.

    1. Bradley Schacht

      This one has come in handy for me on more than one occasion. I’m glad you found it helpful and thank you for visiting!

  1. Query Table Metadata » Bradley Schacht

    […] I hope to create a script library on the site sometime in the near future so that you can come here and download little snippets of code like this from one central location rather than having to parse through blogs if you want something in particular like table metadata or to search through stored procedure text. […]

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>