For years I've been looking for a way to search all the SP's in a specific DB for a specific string. In most cases, it's usually when I'm cleaning up our database and find a table that I think is no longer in use. I then need to search the SP's to make sure the table isn't in use. I believe I found a way that works for me and I hope others would find this useful as well. Comments/critiques/suggestions are all welcome!
My Setup:
We have a table in our 'Master' DB which holds all our customer DB information (server name, connection sting (encrypted), DBName, etc.) so I do not have to use sp_msForEachDB but that could easily be used instead of the opening select to get all DB's. Once the DB's names are retrieved, it's a simple loop:
*Note: All our DB's are prefixed with 'SC_', hence the string concatination in the SP. I also tested this on SQL 2000 and SQL 2008.
My Setup:
We have a table in our 'Master' DB which holds all our customer DB information (server name, connection sting (encrypted), DBName, etc.) so I do not have to use sp_msForEachDB but that could easily be used instead of the opening select to get all DB's. Once the DB's names are retrieved, it's a simple loop:
Code:
DECLARE @DBName varchar(20)
DECLARE @sSQL varchar(500)
DECLARE LoopCursor CURSOR FOR
SELECT DBName AS 'DBName'
FROM [SCMaster].dbo.Configuration c
WHERE Active = 1
OPEN LoopCursor
FETCH NEXT FROM LoopCursor
INTO @DBName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sSQL = '
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM [SC_' + @DBName + '].INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE ''%TEXT TO SEARCH FOR%''
AND ROUTINE_TYPE=''PROCEDURE''
ORDER BY Routine_Name'
EXEC(@sSQL)
FETCH NEXT FROM LoopCursor
INTO @DBName
END
CLOSE LoopCursor
DEALLOCATE LoopCursor
SELECT 'DONE'
*Note: All our DB's are prefixed with 'SC_', hence the string concatination in the SP. I also tested this on SQL 2000 and SQL 2008.