sp_List.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. CREATE PROCEDURE sp_List
  2. @ProcName varchar(200)=''
  3. AS
  4. BEGIN
  5. SET NOCOUNT ON;
  6. DECLARE @Loop INT
  7. DECLARE @Count INT
  8. DECLARE @Database VARCHAR(200)
  9. DECLARE @SQL NVARCHAR(4000)
  10. DECLARE @StoredProcText NVARCHAR(4000)
  11. DECLARE @Search INT
  12. DECLARE @Enter NVARCHAR(2)
  13. CREATE TABLE #Output
  14. (
  15. [RowID] INT IDENTITY(1,1),
  16. [Database] VARCHAR(200),
  17. [StoredProcText] NTEXT
  18. )
  19. DECLARE @Databases TABLE
  20. (
  21. [DatabaseID] INT IDENTITY(1,1),
  22. [Database] VARCHAR(200)
  23. )
  24. DECLARE @ActualOutput TABLE
  25. (
  26. [Database] VARCHAR(200),
  27. [StoredProcText] NTEXT
  28. )
  29. IF LEN(ISNULL(@ProcName,''))=0
  30. BEGIN
  31. DECLARE @Info TABLE
  32. (
  33. Info VARCHAR(100)
  34. )
  35. INSERT INTO @Info (Info) VALUES ('sp_List {StoredProc}')
  36. INSERT INTO @Info (Info) VALUES ('Lists the stored procedure.')
  37. INSERT INTO @Info (Info) VALUES ('If an exact match is found')
  38. INSERT INTO @Info (Info) VALUES ('then the contents of the stored')
  39. INSERT INTO @Info (Info) VALUES ('procedure is listed.')
  40. INSERT INTO @Info (Info) VALUES ('Otherwise, stored procedures that')
  41. INSERT INTO @Info (Info) VALUES ('are similar to it are listed.')
  42. INSERT INTO @Info (Info) VALUES ('Looks in all databases.')
  43. INSERT INTO @Info (Info) VALUES ('')
  44. INSERT INTO @Info (Info) VALUES ('Check out other cool tools like')
  45. INSERT INTO @Info (Info) VALUES ('sp_ColumnsInATable')
  46. INSERT INTO @Info (Info) VALUES ('sp_CreateUserDefinedType')
  47. INSERT INTO @Info (Info) VALUES ('sp_DropColumn')
  48. INSERT INTO @Info (Info) VALUES ('sp_Find')
  49. INSERT INTO @Info (Info) VALUES ('sp_FindColumn')
  50. INSERT INTO @Info (Info) VALUES ('sp_FindInProc')
  51. INSERT INTO @Info (Info) VALUES ('sp_FindInAllProcs')
  52. INSERT INTO @Info (Info) VALUES ('sp_varinsp')
  53. SELECT Info from @Info
  54. RETURN
  55. END
  56. INSERT INTO @Databases ([Database])
  57. SELECT name from sysdatabases
  58. where name not in ('master','tempdb','model','msdb')
  59. and has_dbaccess(name) = 1;
  60. SET @Count = @@ROWCOUNT
  61. SET @Loop = 1
  62. WHILE @Loop <= @Count
  63. BEGIN
  64. SELECT @Database = [Database]
  65. FROM @Databases
  66. WHERE DatabaseID = @Loop
  67. BEGIN TRY
  68. SET @SQL = N'INSERT INTO #Output([Database],StoredProcText) SELECT ''' + @Database + ''',sc.text from ' + @Database + '..sysobjects as so inner join ' + @Database + '..syscomments as sc on so.id = sc.id WHERE so.xtype = ''P'' AND so.NAME = ''' + @ProcName + ''' order BY sc.colid'
  69. EXEC sp_sqlexec @SQL
  70. END TRY
  71. BEGIN CATCH
  72. END CATCH
  73. SET @Loop = @Loop + 1
  74. END
  75. SET @Enter = CHAR(13)
  76. SELECT @Count = COUNT(*) FROM #Output
  77. IF @Count = 0
  78. BEGIN
  79. SELECT @Count = COUNT(*) FROM @Databases
  80. SET @Loop = 1
  81. WHILE @Loop <= @Count
  82. BEGIN
  83. SELECT @Database = [Database]
  84. FROM @Databases
  85. WHERE DatabaseID = @Loop
  86. BEGIN TRY
  87. SET @SQL = N'INSERT INTO #Output([Database],StoredProcText) SELECT ''' + @Database + ''',name from ' + @Database + '..sysobjects WHERE xtype = ''P'' AND NAME LIKE ''%' + @ProcName + '%'' order BY name'
  88. EXEC sp_sqlexec @SQL
  89. END TRY
  90. BEGIN CATCH
  91. END CATCH
  92. SET @Loop = @Loop + 1
  93. END
  94. SELECT [Database],[StoredProcText] as [Matching Stored Procs] FROM #Output
  95. END ELSE
  96. BEGIN
  97. SET @Loop = 1
  98. WHILE @Loop <= @Count
  99. BEGIN
  100. SET @StoredProcText = ''
  101. BEGIN TRY
  102. SELECT @Database = [Database],
  103. @StoredProcText = StoredProcText
  104. FROM #Output
  105. WHERE RowID = @Loop
  106. END TRY
  107. BEGIN CATCH
  108. END CATCH
  109. SET @Search = 1
  110. WHILE @Search > 0 AND LEN(@StoredProcText)>0
  111. BEGIN
  112. SET @Search = 0
  113. SET @Search = CHARINDEX(@Enter,@StoredProcText)
  114. IF @Search > 0
  115. BEGIN
  116. INSERT INTO @ActualOutput ([Database],StoredProcText) VALUES (@Database,SUBSTRING(@StoredProcText,1,@Search-1))
  117. SET @StoredProcText = SUBSTRING(@StoredProcText,@Search+1,LEN(@StoredProcText)-@Search)
  118. END
  119. END
  120. INSERT INTO @ActualOutput ([Database],StoredProcText) VALUES (@Database,@StoredProcText)
  121. SET @Loop = @Loop + 1
  122. END
  123. SELECT [Database],StoredProcText
  124. FROM @ActualOutput
  125. END
  126. DROP TABLE #Output
  127. END
  128. GO