我用的是SQL2008,
有个一个部门表T_Deptment,
有两个字段: Deptment_ID int , Deptment varchar(20)
我怎样才能查找到,所有引用Deptment_ID的表啊?
或者将所有包含Deptment_ID的表列出来啊?

解决方案 »

  1.   

    select name from sysobjects where type='u'
    and id in (select id from syscolumns where name='Deptment_ID')
      

  2.   

    select table_name,
           column_name
    from information_schema.columns
    where column_name='Deptment_ID'
      

  3.   

    select
     name
    from
     sysobjects 
    where
     type='u'
    and
     id in (select id from syscolumns where name='Deptment_ID')
      

  4.   

    查一个数据所有表
    select name from sysobjects where xtype = 'u'
    查所有包含某一列名的所有表。
    select obj.name from sysobjects obj,syscolumns col where col.id = obj.id and col.name='userid'
      

  5.   


    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');
      

  6.   


    -- 查询引用 @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');