Search Instance for a Stored Procedure usage

References:
Recently, I had the need to skim acrossed all the databases on a single instance.  In doing so I figured that the infamous 'sp_msforeachdb' undocumented stored procedure would be useful but was unsure on the methodology or pattern to implement.



So i started first with the ability to simply find the specific SProc within a known database.  This got me started with using various of the practices described in Greg Robidoux's article, with "Method 3 - Using syscomments" being the winner for my needs.

So then i migrated to the pattern i need for skimming all the user databases for the implementation of the SProc, which lead me to work on the implementation using "sp_msforeachdb".  By the end this is the final resulting pattern, with redaction of course.


1:  exec sp_msforeachdb '  
2:  IF ''[?]'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'') BEGIN  
3:       declare @tbl Table (db sysname, sproc varchar(255));  
4:    
5:       insert into @tbl (  
6:            db, sproc  
7:       ) SELECT distinct   
8:            ''[?]'', ss.name + ''.'' + so.name  
9:       from [?].sys.objects so  
10:            inner join [?].sys.schemas ss  
11:                      on so.schema_id = ss.schema_id  
12:            inner join [?].sys.syscomments sc  
13:                      on so.object_id = sc.id  
14:       WHERE charindex(''SPROC_NAME'', text) > 0  
15:    
16:       select * from @tbl  
17:  END  
18:  '  

After some further usage, needed to do a further simplification on the "Results" tab.  For my situation it spammed over 50 databases, which in most cases the SProc i was searching for was consistently located on one database (source) but possibly on 1 or 2 other databases.  So i had to update the ultimately pattern to check for value.  Some may prefer the Exists methodology but in this situation i used count.


1:  exec sp_msforeachdb '  
2:  IF ''[?]'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'') BEGIN  
3:       declare @tbl Table (db sysname, [schema] varchar(255), sproc varchar(255));  
4:    
5:       insert into @tbl (  
6:            db, [schema], sproc  
7:       ) SELECT distinct   
8:            ''[?]'', ss.name, so.name  
9:       from [?].sys.objects so  
10:            inner join [?].sys.schemas ss  
11:                      on so.schema_id = ss.schema_id  
12:            inner join [?].sys.syscomments sc  
13:                      on so.object_id = sc.id  
14:       WHERE charindex(''SPROC_NAME'', text) > 0  
15:    
16:       if (select count(*) from @tbl) > 0 begin  
17:            select db, [schema] + ''.'' + sproc  
18:            from @tbl  
19:            order by [schema], sproc  
20:    
21:       end  
22:  END  
23:  '  
24:    

Comments

Popular posts from this blog

SysInternals - BgInfo for ALL Users

JSON/AJAX Helpers

Acquiring List of controls - Classic JS