为什么这个 T-SQL 会重复结果?
这个查询相当通用,因此应该可以在任何数据库中复制。
我在 TextLine 列中看到重复的 USP...
SELECT DISTINCT b.name as TableName,
STUFF((
SELECT ', ' + OBJECT_NAME(a.object_id)
FROM sys.sql_dependencies a
WHERE a.referenced_major_id = b.object_id
ORDER BY 1
For XML PATH ('')
), 1, 1, '') AS [TextLine]
FROM sys.sql_dependencies a JOIN sys.tables b ON a.referenced_major_id = b.object_id
ORDER BY 1 ASC
最佳答案
将DISTINCT
添加到子查询
SELECT DISTINCT b.name AS TableName,
Stuff((SELECT DISTINCT ', ' + Object_name(a.object_id) --Here
FROM sys.sql_dependencies a
WHERE a.referenced_major_id = b.object_id
ORDER BY 1
FOR XML PATH ('')), 1, 1, '') AS [TextLine]
FROM sys.sql_dependencies a
JOIN sys.tables b
ON a.referenced_major_id = b.object_id
ORDER BY TableName ASC
现在,对于每个表
,只有不同
依赖的对象
将被连接
关于sql - 为什么这个 T-SQL 会重复结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40084437/