View linked server dependencies sql server 2008

2019-02-07 04:41发布

问题:

Does any know how / if it is possible to view all tables/views/stored procedures that depend on a linked server in Sql Server 2008. Basically as if the context menu "View dependencies" was accessible for linked servers?

Any help much appreciated.

Thanks

回答1:

Search for it

SELECT OBJECT_NAME(object_id), *
FROM sys.sql_modules
WHERE definition LIKE '%myLinkedServer%'

Or use the free Red gate SQL Search to do the same with a GUI

There is no table or feature that tracks dependencies between the server-level linked server objects and database-level objects

Note: INFORMATION_SCHEMA views and the legacy syscomments truncate the definition so are unreliable for definition searches.



回答2:

Following @Mitch Wheat's suggestion, here's some sql to run @gbn's answer for all db's on the server. Maybe this will a bit of time for someone.

USE Master;
GO

IF OBJECT_ID('tempdb..#Deps') IS NOT NULL
  BEGIN
    DROP TABLE #Deps
  END

CREATE TABLE #Deps
  (
    [ServerName] [VARCHAR](500) NOT NULL,
    [DatabaseName] [VARCHAR](500) NOT NULL,
    [SchemaName] [VARCHAR](500) NOT NULL,
    [ObjectName] [VARCHAR](MAX) NULL,
    [ObjectId] [INT] NOT NULL,
    [ObjectType] [VARCHAR](500) NOT NULL,
    [DependsOnLinkedServer] [VARCHAR](500) NOT NULL,    
    [definition] [VARCHAR](MAX) NULL
  )

IF OBJECT_ID('tempdb..#Queries') IS NOT NULL
  BEGIN
    DROP TABLE #Queries
  END

SELECT
  REPLACE('INSERT INTO #Deps 
     (
       [ServerName]       
       ,[DatabaseName]
       ,[SchemaName]
       ,[ObjectName]
       ,[ObjectType]
       ,[ObjectId]
       ,[DependsOnLinkedServer]
       ,[definition]
     )
     SELECT
       @@SERVERNAME,
       ''?'' AS DatabaseName,
       s.name AS SchemaName,
       o.name AS ObjectName,
       o.type_desc AS ObjectType,
       m.object_id AS ObjectId,
       ''' + srv.name + ''' AS DependsOnLinkedServer,
       m.definition
     FROM
       [?].sys.sql_modules m
       LEFT OUTER JOIN [?].sys.objects o
       ON m.object_id = o.object_id
       LEFT OUTER JOIN [?].sys.schemas s
       ON o.schema_id = s.schema_id
     WHERE     
       definition LIKE ''%' + srv.name + '%''', CHAR(13) + CHAR(10), '') AS Query
INTO
  #Queries
FROM
  sys.servers srv;
GO

DECLARE @Query AS VARCHAR(MAX)
DECLARE LinkedServerCursor CURSOR FAST_FORWARD
FOR
  SELECT
    Query
  FROM
    #Queries

OPEN LinkedServerCursor
FETCH NEXT FROM LinkedServerCursor INTO @Query;

WHILE @@FETCH_STATUS = 0
  BEGIN  
    EXECUTE master.sys.sp_MSforeachdb @Query

    FETCH NEXT FROM LinkedServerCursor INTO @Query;
  END

CLOSE LinkedServerCursor;
DEALLOCATE LinkedServerCursor; 

GO

SELECT
  ServerName,
  DatabaseName,
  ObjectName,
  '[' + ServerName + '].[' + DatabaseName + '].[' + SchemaName + '].[' + ObjectName + ']' AS QualifiedObjectName,
  DependsOnLinkedServer,
  ObjectType  
FROM
  #Deps
ORDER BY
  ServerName,
  DatabaseName,
  ObjectName


回答3:

I would recommend changing the middle part of the query to be as below to ensure only when it is used in a query you get a hit. If not you will get a lot of false positives whenever the servername is mentioned.

WHERE     
definition LIKE ''%\[' + srv.name + '\].%''
or definition LIKE ''%' + srv.name + '.%''', CHAR(13) + CHAR(10), '') AS Query