欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Sql Server 查询外键对应的Table 的通用方法

程序员文章站 2023-11-23 11:04:58
'@FK_OEE_DEFE_REFERENCE_SYSTEM_UE'为外键的名称 ......

 

select osub.name as [子表名称] ,
            fk.name as [外键名称] ,
            subcol.name as [子表列名] ,
            omain.name as [主表名称] ,
            maincol.name as [主表列名]
from sys.foreign_keys fk
            join sys.all_objects osub on ( fk.parent_object_id = 
            osub.object_id )
            join sys.all_objects omain on ( fk.referenced_object_id 
            = omain.object_id )
            join sys.foreign_key_columns fkcols on ( fk.object_id = 
            fkcols.constraint_object_id )
            join sys.columns subcol on ( osub.object_id = 
            subcol.object_id
            and fkcols.parent_column_id = subcol.column_id
            )
            join sys.columns maincol on ( omain.object_id = 
            maincol.object_id
            and fkcols.referenced_column_id = maincol.column_id
            )
where 
           fk.name like '@fk_oee_defe_reference_system_ue';

 

'@fk_oee_defe_reference_system_ue'为外键的名称