Following stored procedure can be used to search any string in the definition of any stored procedure in all the user databases in a given SQL server instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
USE [master]; GO CREATE PROCEDURE [dbo].[usp_Search_Definition](@SearchString NVARCHAR(1000)) AS SET NOCOUNT ON DECLARE @SQL NVARCHAR(MAX) IF OBJECT_ID('tempdb..#Search_results ') IS NOT NULL DROP TABLE #Search_results CREATE TABLE #Search_results ( Servername NVARCHAR(128), db NVARCHAR(128), objectname NVARCHAR(128), xtype NVARCHAR(10), definition NVARCHAR(MAX) ) DECLARE #databases CURSOR FOR SELECT NAME FROM master..sysdatabases where dbid>4 DECLARE @database_name varchar(128) OPEN #databases FETCH #databases INTO @database_name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @SQL = 'INSERT INTO #Search_results ' SELECT @SQL = @SQL + 'SELECT @@Servername AS Servername,''' + @database_name+ ''' AS db, o.name,o.xtype,m.definition ' SELECT @SQL = @SQL + ' FROM '+@database_name+'.sys.sql_modules m ' SELECT @SQL = @SQL + ' INNER JOIN '+@database_name+'..sysobjects o ON m.object_id=o.id' SELECT @SQL = @SQL + ' WHERE [definition] LIKE ''%'+@SearchString+'%''' EXEC(@SQL) FETCH #databases INTO @database_name END CLOSE #databases DEALLOCATE #databases SELECT Servername AS SQLServer,db as Database_Name, objectname as Object_Name, Definition FROM #Search_results WHERE xtype='P' ORDER BY db,objectname DROP TABLE #Search_results |
Leave a Comment