sp_FindInAllProcs.txt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. CREATE PROCEDURE sp_FindInAllProcs
  2. @Text1 VARCHAR(200)='',
  3. @Text2 VARCHAR(200)=''
  4. AS
  5. BEGIN
  6. SET NOCOUNT ON;
  7. DECLARE @Loop INT
  8. DECLARE @Count INT
  9. DECLARE @Database VARCHAR(200)
  10. DECLARE @SQL NVARCHAR(4000)
  11. DECLARE @Databases TABLE
  12. (
  13. [DatabaseID] INT IDENTITY(1,1),
  14. [Database] VARCHAR(200)
  15. )
  16. CREATE TABLE #Output
  17. (
  18. [Database] VARCHAR(200),
  19. [ProcName] VARCHAR(200),
  20. [Sample1] VARCHAR(200),
  21. [Sample2] VARCHAR(200)
  22. )
  23. IF LEN(ISNULL(@Text1,'')) = 0 AND LEN(ISNULL(@Text2,'')) = 0
  24. BEGIN
  25. DECLARE @Info TABLE
  26. (
  27. Info VARCHAR(100)
  28. )
  29. INSERT INTO @Info (Info) VALUES ('sp_FindInAllProcs text1,text2 (optional)')
  30. INSERT INTO @Info (Info) VALUES ('Searches all stored procedures in all databases for text1 and text2')
  31. INSERT INTO @Info (Info) VALUES ('Lists the database, stored procedure, and a sample of the found text')
  32. INSERT INTO @Info (Info) VALUES ('')
  33. INSERT INTO @Info (Info) VALUES ('Check out other cool tools like')
  34. INSERT INTO @Info (Info) VALUES ('sp_ColumnsInATable')
  35. INSERT INTO @Info (Info) VALUES ('sp_CreateUserDefinedType')
  36. INSERT INTO @Info (Info) VALUES ('sp_DropColumn')
  37. INSERT INTO @Info (Info) VALUES ('sp_Find')
  38. INSERT INTO @Info (Info) VALUES ('sp_FindColumn')
  39. INSERT INTO @Info (Info) VALUES ('sp_FindInProc')
  40. INSERT INTO @Info (Info) VALUES ('sp_List')
  41. INSERT INTO @Info (Info) VALUES ('sp_varinsp')
  42. SELECT Info from @Info
  43. RETURN
  44. END
  45. INSERT INTO @Databases ([Database])
  46. SELECT name from sysdatabases where name not in ('master','tempdb','model','msdb')
  47. SET @Count = @@ROWCOUNT
  48. SET @Loop = 1
  49. WHILE @Loop <= @Count
  50. BEGIN
  51. SELECT @Database = [Database]
  52. FROM @Databases
  53. WHERE DatabaseID = @Loop
  54. IF LEN(ISNULL(@Text2,''))=0
  55. BEGIN
  56. SET @SQL = N'INSERT INTO #Output([Database],ProcName,Sample1,Sample2) SELECT ''' + @Database + ''',so.name,SUBSTRING(sc.text,CHARINDEX('''+@Text1+''',sc.text)-20,40),'''' FROM ' + @Database + '..sysobjects as so inner join ' + @Database + '..syscomments AS SC on so.id = sc.id WHERE so.xtype =''P'' AND sc.text like ''%'+@Text1+'%'''
  57. END ELSE
  58. BEGIN
  59. SET @SQL = N'INSERT INTO #Output([Database],ProcName,Sample1,Sample2) SELECT ''' + @Database + ''',so.name,SUBSTRING(sc.text,CHARINDEX('''+@Text1+''',sc.text)-20,40),SUBSTRING(sc.text,CHARINDEX('''+@Text2+''',sc.text)-20,40) as Sample2 FROM ' + @Database + '..sysobjects as so inner join ' + @Database + '..syscomments AS SC on so.id = sc.id WHERE so.xtype =''P'' AND sc.text like ''%'+@Text1+'%'' AND sc.text like ''%'+@Text2+'%'''
  60. END
  61. EXEC sp_sqlexec @SQL
  62. SET @Loop = @Loop + 1
  63. END
  64. IF LEN(ISNULL(@Text2,''))=0
  65. BEGIN
  66. SELECT [Database],ProcName,Sample1 as [Sample] FROM #Output ORDER BY [Database],ProcName
  67. END ELSE
  68. BEGIN
  69. SELECT [Database],ProcName,Sample1,Sample2 FROM #Output ORDER BY [Database],ProcName
  70. END
  71. DROP TABLE #Output
  72. END
  73. GO