sql表信息查询
查询每个表的字段及相关信息
SELECT
TableName = case when a.colorder=1 then d.name else '' end,
TableInstruction = case when a.colorder=1 then isnull(f.value,'') else '' end,
FieldIndex = a.colorder,
FieldName = a.name,
Identify = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, ----标识
MajorKey = 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 '√' else '' end,
DataType = b.name,
Bytesoccupied = a.length, ----占用字节数
FieldLength = COLUMNPROPERTY(a.id,a.name,'PRECISION'),----长度
DecimalsNum = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),----小数位数
CanBeNull = case when a.isnullable=1 then '√'else '' end,
DefaultValue = isnull(e.text,''),
FieldInstruction = 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
where
d.name=@Table_Name --如果只查询指定表,加上此where条件,tablename是要查询的表名;去除where条件查询所有的表信息
order by
a.id,a.colorder