2016-07-28 25 views
3

データベースに無効な依存関係をすべて返すスクリプトがあります。しかし、このスクリプトは多くの重複を返します。だから私は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'.

私はテストのために結果の各列に集計関数を追加しようとしましたが、まだきた同じエラーが。

+0

を(oa.x oa.y oa.z探し) + ISNULL(QUOTENAME(referenced_database_name)+ '、') + ISNULL(QuoteName(referenced_schema_name)+ '。'、 '') + QuoteName(referenced_entity_name) ' – Matt

+0

@Matt、はい、 'GROUP BY'が役に立ちます。エイリアスによってGROUP BYが動作すると常に思います... – demo

+0

'GROUP BY'は機能しましたか? – Matt

答えて

4

GROUP BY実際のフィールド名はエイリアスではありません。

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')) 
     ) 
GROUP BY ISNULL(QuoteName(referenced_server_name) + '.', '') + ISNULL(QuoteName(referenced_database_name) + '.', '') + ISNULL(QuoteName(referenced_schema_name) + '.', '') + QuoteName(referenced_entity_name) 
ORDER BY [this Object...], 
[... depends ON this missing entity name] 
go 
1

APPLY演算子を使用して、クエリの以前のエイリアスにアクセスすることができます。 `ISNULL BY DISTINCT`または`グループを選択し(QUOTENAME(referenced_server_name)+ ''、 '')

SELECT 
     o.type_desc, sed.referenced_class_desc 
     , oa.x , oa.y , oa.z 
FROM sys.sql_expression_dependencies AS sed 
    LEFT JOIN sys.objects o ON sed.referencing_id = o.object_id 
    OUTER APPLY (
     SELECT 
       ISNULL(QUOTENAME(sed.referenced_server_name) + '.', '') 
      + ISNULL(QUOTENAME(sed.referenced_database_name) + '.', '') 
      + ISNULL(QUOTENAME(sed.referenced_schema_name) + '.', '') 
      + QUOTENAME(sed.referenced_entity_name)     AS x 
      , QUOTENAME(OBJECT_SCHEMA_NAME(referencing_id)) 
      + '.' 
      + QUOTENAME(OBJECT_NAME(referencing_id))     AS y 
      , 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 z 
    ) AS OA 
WHERE (is_ambiguous = 0) 
     AND (oa.x 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')) 
      ) 
GROUP BY 
     o.type_desc, sed.referenced_class_desc 
     , oa.x , oa.y , oa.z 
; 
関連する問題