34,592
社区成员
发帖
与我相关
我的任务
分享
-- 查询引用 @tabname 表上 @colname 列的表和列
declare @tabname sysname, @colname sysname;
select OBJECT_NAME(parent_object_id) table_name,
COL_NAME(OBJECT_ID(@tabname),parent_column_id) column_name
from sys.foreign_key_columns
where referenced_object_id=OBJECT_ID(@tabname) and
referenced_column_id=COLUMNPROPERTY(OBJECT_ID(@tabname),@colname,'columnid');
select OBJECT_NAME(parent_object_id)
from sys.foreign_key_columns
where referenced_object_id=OBJECT_ID('T_Deptment') and
referenced_column_id=(select column_id from sys.columns
where [object_id]=OBJECT_ID('T_Deptment') and name='Deptment_ID');
sp_depends T_Deptment
select
name
from
sysobjects
where
type='u'
and
id in (select id from syscolumns where name='Deptment_ID')
select table_name,
column_name
from information_schema.columns
where column_name='Deptment_ID'
sp_fkeys
select name from sysobjects where type='u'
and id in (select id from syscolumns where name='Deptment_ID')
sp_fkey 'T_Deptment'