日期:2014-05-17  浏览次数:20654 次

求一个语句查表的外键,表名,列名,引用的表名和引用的列名。
这个脚本列出名称的外键,表名,列名,引用的表名和引用的列名。

------解决方案--------------------
SQL code
;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_i