http://www.sqlservercentral.com/blogs/glennberry/2009/11/23/some-ways-to-look-for-things-in-sql-server-modules/

Printed 2014/07/29 04:27PM

Some Ways To Look For Things in SQL Server Modules

By GlennBerry, 2009/11/23

If you have a medium to large database in SQL Server, with lots of stored procedures, functions or views, it can sometimes be difficult to keep track of whether you have used certain options (such as RECOMPILE).  The queries below show some different things you can discover by querying sys.sql_modules and sys.objects.

    -- List all modules in the current database
    SELECT OBJECT_NAME(sm.[object_id]) AS [object_name], o.[type], 
           o.type_desc, sm.[definition]
    FROM sys.sql_modules AS sm
    INNER JOIN sys.objects AS o 
    ON sm.[object_id] = o.[object_id]
    ORDER BY o.[type];
    
    -- Look for modules that use a specific term (Method 1)
    SELECT OBJECT_NAME(sm.[object_id]) AS [Object Name], sm.[definition]
    FROM sys.sql_modules AS sm
    WHERE sm.[definition] LIKE '%SQLCONTENT01%';
    
    -- Look for modules that use a specific term (Method 2)
    SELECT OBJECT_NAME(id) AS [Object Name], [text]
    FROM sys.syscomments 
    WHERE [text] LIKE '%SQLCONTENT01%'
    
    -- Find stored procedures that have WITH RECOMPILE set
    SELECT OBJECT_NAME(sm.[object_id]) AS [Object Name], 
           sm.[definition], sm.is_recompiled
    FROM sys.sql_modules AS sm
    INNER JOIN sys.objects AS o 
    ON sm.[object_id] = o.[object_id]
    WHERE sm.is_recompiled = 1
    AND o.type_desc = 'SQL_STORED_PROCEDURE';

Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.