一个简单的查找外键 以及外键的表名、列名、引用的主表名及列名语句

sql 语句查找外键 以及对应的外键表名、列名和引用的主表名、列名

;WITH  CTE
        AS (
            SELECT  OBJECT_NAME(constraint_object_id) Constraint_Name , OBJECT_NAME(parent_object_id) Table_Name , C.name Column_Name
            FROM    sys.foreign_key_columns FK
            INNER JOIN sys.columns C
            ON      FK.parent_object_id = C.object_id
                    AND FK.parent_column_id = C.column_id
           )
  SELECT  C.Constraint_Name , C.Table_Name , C.Column_Name , OBJECT_NAME(FK.referenced_object_id) Referenced_Table_Name , SC.name Referenced_Column_Name
  FROM    CTE C
  INNER JOIN sys.foreign_key_columns FK
  ON      C.Constraint_Name = OBJECT_NAME(FK.constraint_object_id)
  INNER JOIN sys.columns SC
  ON      FK.referenced_object_id = SC.object_id
          AND FK.referenced_column_id = SC.column_id


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页