Technical Article

Script to find a string in SP/Function/View SQL

,

I mark my SQL with a 'Todo' symbol whenever I decide something needs work, but I don't have the time to do it right away. So I wrote this usp so that I can get a quick list of stored procedures, function and views that need some extra work.

It also comes in handy whenever you have to find a certain character sequence. I had to find everything with '365' in it cause one of my clients liked to hardcode certain values. 🙂

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:jke
-- Create date: 2006-Sep-14
-- Description:Returns a list of Stored Procedures, Views and Functions
--              that contain the SearchCriterium
-- =============================================
CREATE PROCEDURE [dbo].[suspSearchInSQL] 
@SearchCriterium nvarchar(MAX) = '@@@' /*Default value = My own 'ToDo' symbol*/AS
BEGIN

SET NOCOUNT ON;

    SELECT objects.name AS [name], 
        objects.type_desc AS [type], 
        all_sql_modules.definition AS sql
    FROM sys.all_sql_modules 
    INNER JOIN sys.objects
        ON objects.object_id = all_sql_modules.object_id
    WHERE all_sql_modules.definition LIKE '%' + @SearchCriterium + '%'
        AND objects.name != Object_Name(@@Procid) /* Exclude this Stored Procedure from the resultset*/    ORDER BY [type],
        [name]

END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating