• Just a couple of changes are required for this to work properly in my environment.

    I added a where clause on kerser2 WHERE ISREMOTE = '1'

    And I changed the search of syscomments to add a space before sp_srv_name and a "." after it. There were too many times that the server name was somewhere else in the procedure but not being used as a qualifier.

    set @sql = 'insert into LinkedServerDependencies select '''+@sp_db_name+''' as [Database], name as DependantObject, '''+@sp_srv_name+''' as LinkedServer from '+@sp_db_name+'..sysobjects where id in (select id from '+@sp_db_name+'..syscomments where text like ''% '+@sp_srv_name+'.%'')'

    That seemed to fix it up nicely.

    Thanks for this as I was in need of exactly this to clean up some security issues.