我有腳本返回數據庫中的所有無效依賴關係。但是這個腳本會返回許多重複項。所以我只想看到DISTINCT
結果。查找DISTINCT缺少SQL依賴關係
/*
modified version of script from http://michaeljswart.com/2009/12/find-missing-sql-dependencies/
Added columns for object types & generated refresh module command...
filter out user-define types: http://stackoverflow.com/questions/2330521/find-broken-objects-in-sql-server
*/
SELECT TOP (100) PERCENT
QuoteName(OBJECT_SCHEMA_NAME(referencing_id)) + '.' + QuoteName(OBJECT_NAME(referencing_id)) AS [this Object...],
o.type_desc,
ISNULL(QuoteName(referenced_server_name) + '.', '')
+ ISNULL(QuoteName(referenced_database_name) + '.', '')
+ ISNULL(QuoteName(referenced_schema_name) + '.', '')
+ QuoteName(referenced_entity_name) AS [... depends ON this missing entity name]
,sed.referenced_class_desc
,case when o.type_desc in('SQL_STORED_PROCEDURE' ,'SQL_SCALAR_FUNCTION' ,'SQL_TRIGGER' ,'VIEW')
then 'EXEC sys.sp_refreshsqlmodule ''' + QuoteName(OBJECT_SCHEMA_NAME(referencing_id)) + '.' + QuoteName(OBJECT_NAME(referencing_id)) + ''';'
else null
end as [Refresh SQL Module command]
FROM sys.sql_expression_dependencies as sed
LEFT JOIN sys.objects o
ON sed.referencing_id=o.object_id
WHERE (is_ambiguous = 0)
AND (OBJECT_ID(ISNULL(QuoteName(referenced_server_name) + '.', '')
+ ISNULL(QuoteName(referenced_database_name) + '.', '')
+ ISNULL(QuoteName(referenced_schema_name) + '.', '')
+ QuoteName(referenced_entity_name)) IS NULL)
AND NOT EXISTS
(SELECT *
FROM sys.types
WHERE types.name = referenced_entity_name
AND types.schema_id = ISNULL(SCHEMA_ID(referenced_schema_name), SCHEMA_ID('dbo'))
)
ORDER BY [this Object...],
[... depends ON this missing entity name]
go
這是腳本。
我試圖加入GROUP BY [... depends ON this missing entity name]
但我得到以下錯誤:
Invalid column name '... depends ON this missing entity name'.
我試圖聚合函數結果添加到每一列只是爲了測試,但還是同樣的錯誤。
'SELECT DISTINCT'或'GROUP BY ISNULL(QUOTENAME(referenced_server_name)+ '', '') + ISNULL(QUOTENAME(referenced_database_name)+ ''','') + ISNULL(QuoteName(referenced_schema_name)+'。','') + QuoteName(referenced_entity_name)' – Matt
@Matt,是的,'GROUP BY'有幫助。始終認爲GROUP BY的別名應該起作用... – demo
「GROUP BY」有效嗎? – Matt