web-dev-qa-db-fra.com

Rechercher la dépendance de colonne

Comment trouver des objets qui dépendent d'une colonne de table particulière.

Ex:

Tableau: SomeTable

Cols: col1 pk, col2, col3

Je veux trouver tous les objets qui dépendent de col1 (Pk)

19
Sreedhar

Essayez cette requête, elle vous donnera des résultats que je pense que vous recherchez.
Pour filtrer, recherchez la valeur dans la colonne c1.name ou c2.name.
Pour rechercher toutes les références à une certaine colonne, utilisez le nom c2.name pour le nom de la colonne et le OBJECT_NAME (k.referenced_object_id) comme table contenant la colonne c2 :)

Bonne chance!


    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

Le script de @ NoFuchsGavin fonctionne généralement très bien mais a quelques limitations en raison de problèmes avec sysdepends (voir ce billet de blog de Pinal Dave pour un exemple où cela donne des résultats incorrects).

Microsoft suggère également d'éviter d'utiliser sysdepends dans les nouveaux travaux de développement.

On peut donc utiliser sys.dm_sql_referencing_entities et sys.dm_sql_referenced_entities comme suggéré ici .

Cependant, j'ai remarqué que cela exclut parfois les références de colonne en raison de referenced_minor_name étant NULL. J'ai donc ajouté une autre condition qui peut introduire des faux positifs mais garantit que les références de colonne ne sont pas omises de l'ensemble de résultats.

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

Le script ci-dessus est basé sur la réponse de @ NoFuchsGavin et ce billet de blog .

Je suis intéressé de savoir si quelqu'un a réussi à trouver un meilleur moyen qui n'introduise pas de faux négatifs ou positifs.

16
Rachel

Cela devrait fonctionner!

        -- 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

Le SQL fourni dans la réponse acceptée ci-dessus doit inclure une condition de jointure supplémentaire entre sys.foreign_keys et sys.foreign_key_columns. Voir la ligne commençant par "et" ci-dessous:

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

Pour référence, voici le script complet avec la jointure modifiée:

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
Christopher Cullum

Remplacez simplement {0} et {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

Essayez ceci: cela donnera tous les noms d'objets qui font référence à Pk de votre table.

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

Rechercher des dépendances de colonnes spécifiques

 SELECT OBJECT_NAME (referencing_id), 
 Referenced_database_name, 
 Referenced_schema_name, 
 Referenced_entity_name 
 FROM sys.sql_expression_dependencies 
 WHERE OBJECT_NAME (referenced_id) = 'table_name' 
 AND OBJECT_DEFINITION (referencing_id) LIKE '% field_name%'; 
0
Kris Khairallah

Cela devrait faire l'affaire:

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>';

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

ou, pour afficher toutes les dépendances sur une table, utilisez

EXEC sp_depends <TABLE_NAME>

source: https://msdn.Microsoft.com/en-us/library/ms189487.aspx

0
isapir

Il fonctionne à 100% très bien. Essayez de l'utiliser. Cela vous donnera des détails supplémentaires. Merci

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