所有(用户定义的标量)函数及其依赖项的列表

时间:2017-02-28 14:41:40

标签: sql sql-server tsql ssms sql-server-2014

我试图选择数据库中所有函数及其依赖项的列表。

(如果我手动查看依赖关系并选择依赖于[function]'选项的'对象,则会出现依赖关系。)

我的主要问题是将每个函数的所有依赖项汇总到一行。

我已尝试使用stuff,但出于某种原因,我无法在此背景下使用它。

select 
    o.name
    , stuff((select N', ' + Name from sys.objects o3 where o3.object_id = o2.object_id for xml path ('')), 1, 1, N'') as Dependencies
from sys.objects o
    left join sys.sql_expression_dependencies sed
    on object_id = referenced_id
    left join sys.objects o2
    on referencing_id = o2.object_id
where o.type='FN' 
order by o.name

我哪里错了?

1 个答案:

答案 0 :(得分:4)

这可能对您有用:

SELECT
     O.Name
    ,T.Dependencies
FROM sys.Objects O
OUTER APPLY
(
    SELECT SUBSTRING(
        (
        SELECT ',' + OBJECT_NAME(D.referenced_id)
        FROM sys.SQL_Expression_Dependencies D
        WHERE D.referencing_id = O.Object_ID
        GROUP BY OBJECT_NAME(D.referenced_id)
        ORDER BY OBJECT_NAME(D.referenced_id)
        FOR XML PATH('')
        )
    ,2,4000) AS Dependencies
) T
WHERE O.Type = 'FN'
ORDER BY O.Name
;

如果没有,请告诉我有关遗失的内容,我可以从那里开始。祝好运! :)