sp_Find.txt 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. CREATE PROCEDURE sp_Find
  2. @StoredProc 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. CREATE TABLE #Output
  11. (
  12. [Database] VARCHAR(200),
  13. [Item] VARCHAR(200),
  14. [Type] VARCHAR(50)
  15. )
  16. DECLARE @Databases TABLE
  17. (
  18. [DatabaseID] INT IDENTITY(1,1),
  19. [Database] VARCHAR(200)
  20. )
  21. IF LEN(ISNULL(@StoredProc,'')) = 0
  22. BEGIN
  23. DECLARE @Info TABLE
  24. (
  25. Info VARCHAR(100)
  26. )
  27. INSERT INTO @Info (Info) VALUES ('sp_Find {Item}')
  28. INSERT INTO @Info (Info) VALUES ('Searches all databases for a table name,')
  29. INSERT INTO @Info (Info) VALUES ('column name, or stored procedure name')
  30. INSERT INTO @Info (Info) VALUES (' containing {Item}')
  31. INSERT INTO @Info (Info) VALUES ('')
  32. INSERT INTO @Info (Info) VALUES ('Check out other cool tools like')
  33. INSERT INTO @Info (Info) VALUES ('sp_ColumnsInATable')
  34. INSERT INTO @Info (Info) VALUES ('sp_CreateUserDefinedType')
  35. INSERT INTO @Info (Info) VALUES ('sp_DropColumn')
  36. INSERT INTO @Info (Info) VALUES ('sp_FindColumn')
  37. INSERT INTO @Info (Info) VALUES ('sp_FindInProc')
  38. INSERT INTO @Info (Info) VALUES ('sp_FindInAllProcs')
  39. INSERT INTO @Info (Info) VALUES ('sp_List')
  40. INSERT INTO @Info (Info) VALUES ('sp_varinsp')
  41. SELECT Info from @Info
  42. RETURN
  43. END
  44. INSERT INTO @Databases ([Database])
  45. SELECT name from sysdatabases
  46. WHERE name NOT IN ('master','tempdb','model','msdb')
  47. AND has_dbaccess(name) = 1;
  48. SET @Count = @@ROWCOUNT
  49. SET @Loop = 1
  50. WHILE @Loop <= @Count
  51. BEGIN
  52. SELECT @Database = [Database]
  53. FROM @Databases
  54. WHERE DatabaseID = @Loop
  55. SET @SQL = N'INSERT INTO #Output([Database],Item,[Type]) SELECT ''' + @Database + ''',name, case xtype when ''u'' then ''Table'' when ''p'' then ''Procedure'' end from ' + @Database + '..sysobjects WHERE xtype in (''U'',''P'') AND NAME LIKE ''%' + @StoredProc + ''''
  56. EXEC sp_sqlexec @SQL
  57. SET @SQL = N'INSERT INTO #Output([Database],Item,[Type]) SELECT ''' + @Database + ''',so.name+''.''+sc.name, ''Column'' from ' + @Database + '..sysobjects as so inner join ' + @Database + '..syscolumns as sc on so.id = sc.id WHERE so.xtype = ''U'' AND sc.NAME LIKE ''%' + @StoredProc + ''''
  58. EXEC sp_sqlexec @SQL
  59. SET @Loop = @Loop + 1
  60. END
  61. SELECT [Database],Item,[Type]
  62. FROM #Output
  63. ORDER BY [Database],[Type],Item
  64. DROP TABLE #Output
  65. END
  66. GO