web-dev-qa-db-ja.com

列の依存関係を見つける

テーブルの特定の列に依存するオブジェクトを見つける方法。

例:

テーブル:SomeTable

列:列1 pk、列2、列3

Col1(Pk)に依存するすべてのオブジェクトを検索したい

19
Sreedhar

このクエリを試してください。あなたが探していると思われる結果が得られます。
フィルタリングするには、c1.name列またはc2.name列の値を検索します。
特定の列へのすべての参照を探すには、列名にc2.nameを使用し、c2列を保持するテーブルとしてOBJECT_NAME(k.referenced_object_id)を使用します:)

幸運を!


    select  OBJECT_NAME(k.parent_object_id) as parentTable
          , c1.name as parentColumn
          , OBJECT_NAME(k.referenced_object_id) as referencedTable
          , c2.name as referencedColumn
    from    sys.foreign_keys k
            inner join sys.foreign_key_columns f
              on  f.parent_object_id = k.parent_object_id
              and f.constraint_object_id = k.object_id
            inner join sys.columns c1
              on  c1.column_id = f.parent_column_id
              and c1.object_id = k.parent_object_id
            inner join sys.columns c2
              on  c2.column_id = f.referenced_column_id
              and c2.object_id = k.referenced_object_id
    where   c2.name = 'Column'
    and     OBJECT_NAME(k.referenced_object_id) = 'Table'
15
Mark Kremers

@NoFuchsGavinのスクリプトは通常はうまく機能しますが、sysdependsの問題のためにいくつかの制限があります(これが正しくない結果を与える例については、 Pinalal Daveによるこのブログ投稿 を参照してください)。

Microsoft は、新しい開発作業でsysdependsを使用しないこともお勧めします。

したがって、推奨されるようにsys.dm_sql_referencing_entitiesおよびsys.dm_sql_referenced_entitieshere を使用できます。

ただし、referenced_minor_nameがNULLであるため、列参照が除外される場合があることに気付きました。したがって、誤検知を引き起こす可能性があるが、列参照が結果セットから除外されないようにする別の条件を追加しました。

DECLARE @SchemaName sysname = '{0}';
DECLARE @TableName sysname  = '{1}';
DECLARE @ColumnName sysname = '{2}';

SELECT
    @SchemaName + '.' + @TableName                                      AS [USED_OBJECT],
    @ColumnName                                                         AS [COLUMN],
    referencing.referencing_schema_name + '.' + referencing_entity_name AS USAGE_OBJECT,
    CASE so.type
        WHEN 'C' THEN 'CHECK constraint'
        WHEN 'D' THEN 'Default'
        WHEN 'F' THEN 'FOREIGN KEY'
        WHEN 'FN' THEN 'Scalar function' 
        WHEN 'IF' THEN 'In-lined table-function'
        WHEN 'K' THEN 'PRIMARY KEY'
        WHEN 'L' THEN 'Log'
        WHEN 'P' THEN 'Stored procedure'
        WHEN 'R' THEN 'Rule'
        WHEN 'RF' THEN 'Replication filter stored procedure'
        WHEN 'S' THEN 'System table'
        WHEN 'TF' THEN 'Table function'
        WHEN 'TR' THEN 'Trigger'
        WHEN 'U' THEN 'User table' 
        WHEN 'V' THEN 'View' 
        WHEN 'X' THEN 'Extended stored procedure'
    END                                             AS USAGE_OBJECTTYPE,
    so.[type]                                       AS USAGE_OBJECTTYPEID
FROM sys.dm_sql_referencing_entities
    (
        @SchemaName + '.' + @TableName,
        'object'
    ) referencing
    INNER JOIN sys.objects so 
        ON referencing.referencing_id = so.object_id
WHERE
    EXISTS
    (
        SELECT
            *
        FROM
            sys.dm_sql_referenced_entities
            (
                referencing_schema_name + '.' + referencing_entity_name,
                'object'
            ) referenced
        WHERE
            referenced_entity_name = @TableName
            AND 
            (
                referenced.referenced_minor_name LIKE @ColumnName   
                -- referenced_minor_name is sometimes NULL
                -- therefore add below condition (can introduce False Positives)
                OR
                (
                    referenced.referenced_minor_name IS NULL 
                    AND 
                    OBJECT_DEFINITION
                    (
                         OBJECT_ID(referencing_schema_name + '.' + referencing_entity_name)
                    ) LIKE '%' + @ColumnName + '%'
                )
            )
    )
ORDER BY
    USAGE_OBJECTTYPE,
    USAGE_OBJECT

上記のスクリプトは、@ NoFuchsGavinの回答と このブログ投稿に基づいています

誰かが偽陰性や陽性をもたらさないより良い方法を見つけたのかどうか知りたいです。

16
Rachel

これはうまくいくはずです!

        -- Search in All Objects
SELECT OBJECT_NAME(OBJECT_ID),definition
FROM sys.sql_modules
WHERE definition LIKE '%' + 'ColumnToBeSearched' + '%'
Order by 1
GO

-- Search in Stored Procedure Only
SELECT DISTINCT OBJECT_NAME(OBJECT_ID),
object_definition(OBJECT_ID)
FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%' + 'ColumnToBeSearched' + '%'
GO
11
Arpan Mohokar

上記の受け入れられた回答 で提供されるSQLには、sys.foreign_keysとsys.foreign_key_columnsの間に追加の結合条件を含める必要があります。以下の「and」で始まる行を参照してください。

from    sys.foreign_keys k
        inner join sys.foreign_key_columns f
          on  f.parent_object_id = k.parent_object_id
               and f.constraint_object_id = k.object_id

参考までに、結合を修正したスクリプト全体を次に示します。

select  OBJECT_NAME(k.parent_object_id) as parentTable
        , c1.name as parentColumn
        , OBJECT_NAME(k.referenced_object_id) as referencedTable
        , c2.name as referencedColumn
from    sys.foreign_keys k
        inner join sys.foreign_key_columns f
            on  f.parent_object_id = k.parent_object_id
            and f.constraint_object_id = k.object_id
        inner join sys.columns c1
            on  c1.column_id = f.parent_column_id
            and c1.object_id = k.parent_object_id
        inner join sys.columns c2
            on  c2.column_id = f.referenced_column_id
            and c2.object_id = k.referenced_object_id
where   c2.name = 'GUID'
and     OBJECT_NAME(k.referenced_object_id) = 'AuthDomain'
7

単に{0}と{1}を置き換えてください!

declare @tbl_nme as varchar(50)
declare @col_nme as varchar(50)
declare @level int
set @level = 1
set @tbl_nme= '{0}' --TableName 
set @col_nme= '{1}' --ColumnName 



select 
  obj.name as obj_nm
, col.name as col_nm
, depobj.name as dep_obj_nm
, CASE depobj.type
     WHEN 'C' THEN 'CHECK constraint'
     WHEN 'D' THEN 'Default'
     WHEN 'F' THEN 'FOREIGN KEY'
     WHEN 'FN' THEN 'Scalar function' 
     WHEN 'IF' THEN 'In-lined table-function'
     WHEN 'K' THEN 'PRIMARY KEY'
     WHEN 'L' THEN 'Log'
     WHEN 'P' THEN 'Stored procedure'
     WHEN 'R' THEN 'Rule'
     WHEN 'RF' THEN 'Replication filter stored procedure'
     WHEN 'S' THEN 'System table'
     WHEN 'TF' THEN 'Table function'
     WHEN 'TR' THEN 'Trigger'
     WHEN 'U' THEN 'User table' 
     WHEN 'V' THEN 'View' 
     WHEN 'X' THEN 'Extended stored procedure'
  END as dep_obj_type 
, null as dep_col_nm
, depobj.type as dep_obj_typeID
, @level as level
into #temp
from   sysobjects obj 
   join   syscolumns col on obj.id = col.id 
   left   join (sysdepends dep join sysobjects depobj on depobj.id = dep.id) 
         on obj.id = dep.depid 
        and col.colid = dep.depnumber 
where  obj.name = @tbl_nme
   and col.name = @col_nme


while (@@rowcount > 0)
begin
set @level = @level + 1
insert into #temp
select 
  obj.name as obj_nm
, col.name as col_nm
, depobj.name as dep_obj_nm
, CASE depobj.type
     WHEN 'C' THEN 'CHECK constraint'
     WHEN 'D' THEN 'Default'
     WHEN 'F' THEN 'FOREIGN KEY'
     WHEN 'FN' THEN 'Scalar function' 
     WHEN 'IF' THEN 'In-lined table-function'
     WHEN 'K' THEN 'PRIMARY KEY'
     WHEN 'L' THEN 'Log'
     WHEN 'P' THEN 'Stored procedure'
     WHEN 'R' THEN 'Rule'
     WHEN 'RF' THEN 'Replication filter stored procedure'
     WHEN 'S' THEN 'System table'
     WHEN 'TF' THEN 'Table function'
     WHEN 'TR' THEN 'Trigger'
     WHEN 'U' THEN 'User table' 
     WHEN 'V' THEN 'View' 
     WHEN 'X' THEN 'Extended stored procedure'
  END as dep_obj_type 
, null as dep_col_nm
, depobj.type as dep_obj_typeID
, @level as level
from   sysobjects obj 
   join   syscolumns col on obj.id = col.id 
   left   join (sysdepends dep join sysobjects depobj on depobj.id = dep.id) 
         on obj.id = dep.depid 
        and col.colid = dep.depnumber 
where  exists(select 1 from #temp a where obj.name = a.dep_obj_nm and 
col.name = a.dep_col_nm and level = @level - 1 and dep_col_nm is not null)
end

select 
   obj_nm AS 'TABLE',
   col_nm AS 'COLUMN',
   dep_obj_nm AS 'USAGE_OBJECT',
   dep_obj_type AS 'USAGE_OBJECTTYPE',
   dep_obj_typeID AS 'USAGE_OBJECTTYPEID'
from #temp
drop table #temp
7
Spektakulatius

これを試してください:これは、テーブルのPkを参照しているすべてのオブジェクト名を示します。

select  OBJECT_NAME(parent_object_id) from sys.foreign_keys where referenced_object_id = OBJECT_ID('YourTableName')
2
AnandPhadke

特定の列の依存関係を見つける

[.____。 = 'table_name' 
 AND OBJECT_DEFINITION(referencing_id)LIKE '%field_name%'; 
0
Kris Khairallah

これでうまくいくはずです:

SELECT OBJECT_NAME (referencing_id), referencing_id, referenced_id 
FROM  sys.sql_expression_dependencies d
WHERE OBJECT_NAME(d.referenced_id) = '<TABLE_NAME>'
      AND OBJECT_DEFINITION(referencing_id) = '<COLUMN_NAME>';

ソース: http://www.mssqltips.com/sqlservertip/2999/different-ways-to-find-sql-server-object-dependencies/

または、テーブルのすべての依存関係を表示するには、次を使用します

EXEC sp_depends <TABLE_NAME>

ソース: https://msdn.Microsoft.com/en-us/library/ms189487.aspx

0
isapir

100%正常に動作しています。これを使ってみてください。追加の詳細が表示されます。ありがとう

SELECT
FK.TABLE_NAME AS Key_Table,
CU.COLUMN_NAME AS Foreignkey_Column,
PK.TABLE_NAME AS Primarykey_Table,
PT.COLUMN_NAME AS Primarykey_Column,
C.CONSTRAINT_NAME AS Constraint_Name
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON C.CONSTRAINT_NAME = Fk.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (SELECT
i1.TABLE_NAME,
i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
  ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY') PT
  ON PT.TABLE_NAME = PK.TABLE_NAME
WHERE PK.TABLE_NAME = 'HRM_tbEmployee'
AND PT.COLUMN_NAME = 'EmployeeID';
0
ARB