sp_varinsp.txt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. CREATE PROCEDURE sp_varinsp
  2. @proc NVARCHAR(100)=NULL,
  3. @var NVARCHAR(100)=NULL
  4. AS
  5. BEGIN
  6. SET NOCOUNT ON;
  7. DECLARE @Info TABLE
  8. (
  9. ID INT IDENTITY(1,1),
  10. Info NVARCHAR(MAX),
  11. PRIMARY KEY(ID)
  12. );
  13. DECLARE @ProcsToProcess TABLE
  14. (
  15. ID INT IDENTITY(1,1),
  16. DatabaseName NVARCHAR(100),
  17. ProcName NVARCHAR(100),
  18. History NVARCHAR(MAX),
  19. PRIMARY KEY(ID)
  20. );
  21. DECLARE @Databases TABLE
  22. (
  23. ID INT IDENTITY(1,1),
  24. DatabaseName NVARCHAR(100),
  25. PRIMARY KEY(ID)
  26. );
  27. CREATE TABLE #StoredProcs
  28. (
  29. ID INT IDENTITY(1,1),
  30. DatabaseName NVARCHAR(100),
  31. ProcName NVARCHAR(100),
  32. PRIMARY KEY(ID)
  33. );
  34. CREATE TABLE #ProcData
  35. (
  36. ID INT IDENTITY(1,1),
  37. ProcData NVARCHAR(MAX),
  38. PRIMARY KEY(ID)
  39. );
  40. DECLARE @Found TABLE
  41. (
  42. ID INT IDENTITY(1,1),
  43. DatabaseName NVARCHAR(100),
  44. ProcName NVARCHAR(100),
  45. History NVARCHAR(MAX),
  46. Example1 NVARCHAR(50),
  47. Example2 NVARCHAR(50),
  48. Example3 NVARCHAR(50),
  49. Example4 NVARCHAR(50),
  50. Example5 NVARCHAR(50),
  51. Example6 NVARCHAR(50),
  52. PRIMARY KEY(ID)
  53. );
  54. DECLARE @ProcsToProcessCount INT;
  55. DECLARE @ProcsToProcessLoop INT;
  56. DECLARE @DatabaseCount INT;
  57. DECLARE @DatabaseLoop INT;
  58. DECLARE @DatabaseName NVARCHAR(100);
  59. DECLARE @ProcName NVARCHAR(100);
  60. DECLARE @DatabaseName2 NVARCHAR(100);
  61. DECLARE @ProcName2 NVARCHAR(100);
  62. DECLARE @History NVARCHAR(MAX);
  63. DECLARE @SQL NVARCHAR(4000);
  64. DECLARE @ProcSQL NVARCHAR(MAX);
  65. DECLARE @CurrentDB NVARCHAR(100);
  66. DECLARE @ProcDataCount INT;
  67. DECLARE @ProcDataLoop INT;
  68. DECLARE @Example1 NVARCHAR(50);
  69. DECLARE @Example2 NVARCHAR(50);
  70. DECLARE @Example3 NVARCHAR(50);
  71. DECLARE @Example4 NVARCHAR(50);
  72. DECLARE @Example5 NVARCHAR(50);
  73. DECLARE @Example6 NVARCHAR(50);
  74. DECLARE @Search1 INT;
  75. DECLARE @Search2 INT;
  76. IF LEN(ISNULL(@proc,'')) = 0 OR LEN(ISNULL(@var,'')) = 0
  77. BEGIN
  78. INSERT into @Info (Info) VALUES (N'sp_varinsp proc,variable');
  79. INSERT into @Info (Info) VALUES (N'searches proc for variable');
  80. INSERT into @Info (Info) VALUES (N'Recursively searches stored procs that are used in proc.');
  81. INSERT into @Info (Info) VALUES (N'It only searches the current database and DTS_Master.');
  82. INSERT into @Info (Info) VALUES (N'It sometimes takes a few minutes to finish. Be patient.');
  83. INSERT into @Info (Info) VALUES (N'Example: Use FEX_DEV;');
  84. INSERT into @Info (Info) VALUES (N'EXEC sp_varinsp ''pr_iDMSConversion_Acct_v4_LoadXML_r2'',''D-37464'';');
  85. INSERT INTO @Info (Info) VALUES ('Check out other cool tools like');
  86. INSERT INTO @Info (Info) VALUES ('sp_ColumnsInATable')
  87. INSERT INTO @Info (Info) VALUES ('sp_CreateUserDefinedType')
  88. INSERT INTO @Info (Info) VALUES ('sp_DropColumn')
  89. INSERT INTO @Info (Info) VALUES ('sp_Find')
  90. INSERT INTO @Info (Info) VALUES ('sp_FindColumn')
  91. INSERT INTO @Info (Info) VALUES ('sp_FindInProc')
  92. INSERT INTO @Info (Info) VALUES ('sp_FindInAllProcs')
  93. INSERT INTO @Info (Info) VALUES ('sp_List')
  94. SELECT Info FROM @Info ORDER BY ID;
  95. END ELSE
  96. BEGIN
  97. -- Get the list of databases.
  98. SET @CurrentDB = DB_NAME();
  99. INSERT INTO @Databases (DatabaseName)
  100. SELECT [Name]
  101. FROM sysdatabases WHERE [name] NOT IN (N'master',N'model',N'msdb',N'tempdb')
  102. AND [name] IN (@CurrentDB,'DTS_MASTER');
  103. SELECT @DatabaseCount = COUNT(*)
  104. FROM @Databases;
  105. -- Get the list of stored procs for all databases.
  106. SET @DatabaseLoop = 1;
  107. WHILE @DatabaseLoop <= @DatabaseCount
  108. BEGIN
  109. SELECT @DatabaseName = DatabaseName
  110. FROM @Databases
  111. WHERE ID = @DatabaseLoop;
  112. SET @SQL = 'INSERT INTO #StoredProcs (DatabaseName,ProcName) SELECT ''' + @DatabaseName +''',name FROM '+@DatabaseName+'..sysobjects WHERE xtype IN (''P'',''F'')';
  113. exec sp_executesql @SQL;
  114. SET @DatabaseLoop = @DatabaseLoop + 1;
  115. END;
  116. IF NOT EXISTS(SELECT ID FROM #StoredProcs WHERE DatabaseName = @CurrentDB AND ProcName = @proc)
  117. BEGIN
  118. INSERT INTO @Info (Info) VALUES (N'Error: Stored proc');
  119. INSERT INTO @Info (Info) VALUES (@proc);
  120. INSERT INTO @Info (Info) VALUES (N'wasn`t found');
  121. INSERT INTO @Info (Info) VALUES (N'in database');
  122. INSERT INTO @Info (Info) VALUES (@CurrentDB);
  123. SELECT Info FROM @Info ORDER BY ID;
  124. END ELSE
  125. BEGIN
  126. -- Add This one proc into the list of procedures to process.
  127. INSERT INTO @ProcsToProcess (DatabaseName,ProcName,History) VALUES(@CurrentDb,@proc,'');
  128. SET @ProcsToProcessCount = 1;
  129. -- Process all the procs in @ProcsToProcess. More might be added. This is how recursion is handled.
  130. SET @ProcsToProcessLoop = 1;
  131. WHILE @ProcsToProcessLoop <= @ProcsToProcessCount
  132. BEGIN
  133. SELECT @ProcName = ProcName,
  134. @DatabaseName = DatabaseName,
  135. @History = History
  136. FROM @ProcsToProcess
  137. WHERE ID = @ProcsToProcessLoop;
  138. -- GET THE DATA FOR THIS PROCEDURE.
  139. DELETE FROM #ProcData;
  140. SET @SQL = N'INSERT INTO #ProcData (ProcData) SELECT SC.text FROM '+@DatabaseName+'..syscomments AS SC INNER JOIN '+@DatabaseName+'..sysobjects AS SO ON SC.id = SO.id WHERE SO.xtype IN (''P'',''F'') AND SO.name = '''+@ProcName+''' ORDER BY SC.colid';
  141. EXEC sp_executesql @SQL;
  142. SELECT @ProcDataCount = MAX(ID),
  143. @ProcDataLoop = MIN(ID)
  144. FROM #ProcData;
  145. SET @ProcSQL = N'';
  146. WHILE @ProcDataLoop <= @ProcDataCount
  147. BEGIN
  148. SELECT @SQL = ProcData
  149. FROM #ProcData
  150. WHERE ID = @ProcDataLoop;
  151. SET @ProcSQL = @ProcSQL + @SQL;
  152. SET @ProcDataLoop = @ProcDataLoop + 1;
  153. END;
  154. -- Look for other procedures in this procedure.
  155. INSERT INTO @ProcsToProcess (DatabaseName,ProcName,History)
  156. SELECT SP.DatabaseName,SP.ProcName,@History + (CASE WHEN @DatabaseName = @CurrentDB THEN N'-->' ELSE N'-->' + @DatabaseName + 'N..' END) + @ProcName
  157. FROM #StoredProcs as SP
  158. LEFT JOIN @ProcsToProcess AS PP
  159. ON SP.DatabaseName = PP.DatabaseName
  160. AND SP.ProcName = PP.ProcName
  161. WHERE PP.ID IS NULL
  162. AND CHARINDEX(SP.ProcName,@ProcSQL) > 0
  163. AND ((SP.DatabaseName = @DatabaseName)
  164. OR (SP.DatabaseName <> @DatabaseName AND (CHARINDEX(SP.DatabaseName+'..'+SP.ProcName,@ProcSQL) > 0 OR CHARINDEX(SP.DatabaseName+'.dbo.'+SP.ProcName,@ProcSQL) > 0 OR CHARINDEX(SP.DatabaseName+'].[dbo].['+SP.ProcName,@ProcSQL) > 0)));
  165. SET @ProcsToProcessCount = @ProcsToProcessCount + ISNULL(@@ROWCOUNT,0);
  166. SET @Search1 = CHARINDEX(@var,@ProcSQL);
  167. IF @Search1 > 0
  168. BEGIN
  169. -- This variable is used in this procedure.
  170. -- Look for 3 examples of its use.
  171. SET @Example1 = CASE WHEN @Search1 < 25 THEN SUBSTRING(@ProcSQL,1,50) ELSE SUBSTRING(@ProcSQL,@Search1-25,50) END;
  172. SET @Example2 = N'';
  173. SET @Example3 = N'';
  174. SET @Example4 = N'';
  175. SET @Example5 = N'';
  176. SET @Example6 = N'';
  177. SET @Search2 = CHARINDEX(@var,@ProcSQL,@Search1+1);
  178. IF @Search2 > 0
  179. BEGIN
  180. SET @Example2 = CASE WHEN @Search2 < 25 THEN SUBSTRING(@ProcSQL,1,50) ELSE SUBSTRING(@ProcSQL,@Search2-25,50) END;
  181. SET @Search1 = CHARINDEX(@var,@ProcSQL,@Search2+1);
  182. IF @Search1 > 0
  183. BEGIN
  184. SET @Example3 = CASE WHEN @Search1 < 25 THEN SUBSTRING(@ProcSQL,1,50) ELSE SUBSTRING(@ProcSQL,@Search1-25,50) END;
  185. SET @Search2 = CHARINDEX(@var,@ProcSQL,@Search1+1);
  186. IF @Search2 > 0
  187. BEGIN
  188. SET @Example4 = CASE WHEN @Search2 < 25 THEN SUBSTRING(@ProcSQL,1,50) ELSE SUBSTRING(@ProcSQL,@Search2-25,50) END;
  189. SET @Search1 = CHARINDEX(@var,@ProcSQL,@Search2+1);
  190. IF @Search1 > 0
  191. BEGIN
  192. SET @Example5 = CASE WHEN @Search1 < 25 THEN SUBSTRING(@ProcSQL,1,50) ELSE SUBSTRING(@ProcSQL,@Search1-25,50) END;
  193. SET @Search2 = CHARINDEX(@var,@ProcSQL,@Search1+1);
  194. IF @Search2 > 0
  195. BEGIN
  196. SET @Example6 = CASE WHEN @Search2 < 25 THEN SUBSTRING(@ProcSQL,1,50) ELSE SUBSTRING(@ProcSQL,@Search2-25,50) END;
  197. END;
  198. END;
  199. END;
  200. END;
  201. END;
  202. INSERT INTO @Found (DatabaseName,ProcName,History,Example1,Example2,Example3,Example4,Example5,Example6)
  203. VALUES (@DatabaseName,@ProcName,@History + (CASE WHEN @DatabaseName = @CurrentDB THEN N'-->' ELSE @DatabaseName + '-->N..' END) + @ProcName
  204. ,@Example1,@Example2,@Example3,@Example4,@Example5,@Example6);
  205. END;
  206. SET @ProcsToProcessLoop = @ProcsToProcessLoop + 1
  207. END;
  208. SELECT DatabaseName,ProcName,History,Example1,Example2,Example3,Example4,Example5,Example6
  209. FROM @Found
  210. ORDER BY ID;
  211. SELECT DatabaseName,ProcName,History
  212. FROM @ProcsToProcess
  213. ORDER BY ID;
  214. END;
  215. END;
  216. DROP TABLE #StoredProcs;
  217. DROP TABLE #ProcData;
  218. END
  219. GO