我如何检查sql server的“视图”依赖项

时间:2010-04-02 13:28:33

标签: sql-server tsql sql-server-2005 views

有没有办法可以找出使用自定义查询或存储过程的视图正在使用哪些基表?

5 个答案:

答案 0 :(得分:9)

您可以使用sys.dm_sql_referenced_entities函数查找指定视图引用的对象:

SELECT DISTINCT 
referenced_schema_name , 
referenced_entity_name 
FROM sys.dm_sql_referenced_entities ('Sales.vSalesPersonSalesByFiscalYears', 'OBJECT');

enter image description here

此外,还有sys.sql_expression_dependencies系统视图,您可以在其中指定表名和引用对象的类型:

SELECT 
referencing_object_name = o.name, 
referencing_object_type_desc = o.type_desc 
FROM sys.sql_expression_dependencies se 
INNER JOIN sys.objects o 
ON se.referencing_id = o.[object_id] 
WHERE referenced_entity_name = 'Person' AND o.type_desc = 'View'

enter image description here

避免"手册"你还可以使用ApexSQL Clean这个SQL Server工具来查找所有内部和外部依赖项。在结果窗格中选择一个对象,然后查看依赖于所选对象的所有对象,以及所选对象所依赖的对象:

enter image description here

您还可以过滤对象并可视化依赖关系:

enter image description here

免责声明:我作为支持工程师为ApexSQL工作

希望这有帮助

答案 1 :(得分:7)

您可以使用sql_dependencies视图:

select OBJECT_NAME(referenced_major_id) as DependantObject
from sys.sql_dependencies
where object_id = object_id('YourViewName')

以递归方式检索依赖项(例如,如果从视图中选择,则会找到其他视图引用的表):

with deps (child, parent) as (
    select d.object_id, d.referenced_major_id
    from sys.sql_dependencies d
    where d.object_id = object_id('YourViewName')
    union all
    select d.object_id, d.referenced_major_id
    from sys.sql_dependencies d
    inner join deps on deps.parent = d.object_id
)
select OBJECT_NAME(parent)
from deps

这种方法不是万无一失的。例如,如果使用sp_rename重命名对象,则不会更新其依赖项。

答案 2 :(得分:1)

答案 3 :(得分:1)

如果这是你经常做的事情,Red Gate SQL Dependency Tracker(不,我不为他们工作)是一个很好的工具。如果你想尝试,我认为他们有试用期。

答案 4 :(得分:0)

您可以使用SQL-Server> = 2008(R1 +)中的sys.sql_expression_dependencies执行此操作
注意:这将需要30秒或更长时间。

如果想要观看,删除标量+表值函数('FN'和'IF')。

表值函数可以是视图的依赖关系,反之亦然,加上视图/ tvf可能依赖于标量函数。

;WITH TFV_Dependencies AS 
(
    SELECT 
         --OBJECT_SCHEMA_NAME(sysdep.referencing_id) AS Referencing_Schema 
         ReferencingO.object_id AS ReferencingObject_Id 
        ,sysschema.name AS Referencing_Schema 
        ,ReferencingO.name AS Referencing_Object_Name 
        ,sysdep.referenced_schema_name AS Referenced_Schema 
        ,sysdep.referenced_entity_name AS Referenced_Object_Name 
        --,ReferencingO.type
        ,ReferencedO.name AS RefName 
        --,ReferencedO.type AS RefType
    FROM sys.objects as ReferencingO 

    LEFT JOIN sys.schemas AS sysschema 
        ON sysschema.schema_id = ReferencingO.schema_id 

    LEFT JOIN sys.sql_expression_dependencies AS sysdep 
    LEFT JOIN sys.objects as ReferencedO 
        ON ReferencedO.name = sysdep.referenced_entity_name 
        ON sysdep.referencing_id = ReferencingO.object_id 
        AND ReferencingO.name <> sysdep.referenced_entity_name 
        --AND ReferencedO.type <> 'U' 
        AND ReferencedO.type IN ( 'IF', 'FN', 'V' ) 

    WHERE (1=1) 
    --AND ReferencingO.type NOT IN ('PK','F','UQ','SQ','D','IT', 'S')
    --AND ReferencingO.type IN ( 'U', 'V', 'FN', 'IF', 'P') 
    AND ReferencingO.type IN ( 'IF', 'FN', 'V' ) 
    AND ReferencingO.name NOT IN 
    (
         'dt_adduserobject'
        ,'dt_droppropertiesbyid'
        ,'dt_dropuserobjectbyid'
        ,'dt_generateansiname'
        ,'dt_getobjwithprop'
        ,'dt_getobjwithprop_u'
        ,'dt_getpropertiesbyid'
        ,'dt_getpropertiesbyid_u'
        ,'dt_setpropertybyid'
        ,'dt_setpropertybyid_u'
        ,'dt_verstamp006'
        ,'dt_verstamp007'

        ,'sp_helpdiagrams'
        ,'sp_creatediagram'
        ,'sp_alterdiagram'
        ,'sp_renamediagram'
        ,'sp_dropdiagram'

        ,'sp_helpdiagramdefinition'
        ,'fn_diagramobjects'
    ) 

) 

,DependencyGroup AS 
( 
    -- base case 
    SELECT 
         ReferencingObject_Id AS ObjectId 
        ,Referencing_Object_Name AS ObjectName  
        ,Referencing_Schema AS ObjectSchema 
        ,1 AS Lvl 
    FROM TFV_Dependencies 
    WHERE 1=1 
    AND Referenced_Object_Name IS NULL 

    -- recursive case
    UNION ALL 

   SELECT 
         d.ReferencingObject_Id AS ObjectId 
        ,d.Referencing_Object_Name AS ObjectName 
        ,d.Referencing_Schema AS ObjectSchema 
        ,r.Lvl + 1 AS Lvl 
    FROM TFV_Dependencies AS d 
    INNER JOIN DependencyGroup AS r 
        ON r.ObjectName = d.Referenced_Object_Name 
) 
,
CTE AS 
(
    SELECT TOP 999999999999999999 
         MAX(Lvl) AS Lvl 
        ,ObjectId 
        ,ObjectSchema
        ,ObjectName 
        --,'DELETE FROM [' + REPLACE(ObjectName, '''', '''''') + ']; ' AS DeleteCmd 
    FROM DependencyGroup 

    GROUP BY ObjectId, ObjectSchema, ObjectName 
)

SELECT 
     CTE.Lvl
    ,CTE.ObjectSchema AS SPECIFIC_SCHEMA 
    ,CTE.ObjectName AS SPECIFIC_NAME 
    ,sysSqlModules.definition AS ROUTINE_DEFINITION 
FROM CTE 

LEFT JOIN sys.sql_modules AS sysSqlModules 
    ON sysSqlModules.object_id = CTE.ObjectId 

ORDER BY Lvl, ObjectSchema, ObjectName 

OPTION (MAXRECURSION 0)