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

 

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’为外键的名称

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐