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

dxxyhpgq  于 2023-01-25  发布在  SQL Server
关注(0)|答案(6)|浏览(296)

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

monwx1rj

monwx1rj1#

可以使用www.example.com_sql_referenced_entities函数查找指定视图引用的对象:sys.dm_sql_referenced_entities function to find objects referenced by a specified view:

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

此外,还有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'

为了避免"手动"工作,您还可以使用ApexSQL Clean,这是一个SQL Server工具,可以查找所有内部和外部依赖关系。在结果窗格中选择一个对象,然后查看依赖于所选对象的所有对象,以及所选对象依赖的对象:

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

  • 免责声明:我是ApexSQL的支持工程师 *

希望这能帮上忙

dldeef67

dldeef672#

您可以使用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重命名对象,则不会更新其依赖项。

hwamh0ep

hwamh0ep3#

如果这是你经常做的事情,红门SQL依赖跟踪(不,我不为他们工作)是一个伟大的工具。我认为他们有一个试用期,如果你想尝试它。

cl25kdpy

cl25kdpy5#

您可以在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)
jgwigjjp

jgwigjjp6#

SELECT distinct
    v.table_name AS ViewName,
    re.referenced_entity_name
FROM information_schema.views v
CROSS APPLY sys.dm_sql_referenced_entities('dbo.' + v.table_name,'OBJECT') re
WHERE re.referenced_class = 1

这对我有用!

相关问题