根据字段名查找数据库中包含的所有表

--Author HandLoong 
--version 1.1
with t as (SELECT  
  TableName = case when a.colorder=1 then d.name else d.name end,
  TableDesc = case when a.colorder=1 then f.value else f.value end,
  ID = a.colorder,
  FieldName = a.name,
  Bs = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '1'else '0' end,
  Primarkey = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
  SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '1' else '0' end,
  FieldType = b.name,
  UserByte = a.length,
  Amount = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
  DoubleBit = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
  IsBeNull = case when a.isnullable=1 then '1'else '0' end,
  DefaultValue = isnull(e.text,''),
  FieldDesc = isnull(g.[value],'')
FROM  
  syscolumns a
left join  
  systypes b  
on  
  a.xusertype=b.xusertype
inner join  
  sysobjects d  
on  
  a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join  
  syscomments e  
on  
  a.cdefault=e.id
left join  
sys.extended_properties g  
on  
  a.id=G.major_id and a.colid=g.minor_id   
left join  


sys.extended_properties f
on  
  d.id=f.major_id and f.minor_id=0
  )select * from t 
  where  t.FieldName like '%UserId%'
  order by tablename

  最后代码  t.FieldName like '%UserId%' 可以查找数据库中包含 UserId 字段的表

posted @ 2018-11-20 15:56  邓振振  阅读(324)  评论(0编辑  收藏