SQLServer2005+获取表结构信息

时间:2021-08-29 15:09:36

SELECT d.name TableName,
a.colorder FieldNo,a.name FieldName,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '1'else '0' end) IsIdentity,
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in (SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in (SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in (SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))
) AND (xtype = 'PK')
) > 0 then '1' else '0' end) IsPrimaryKey,
b.name as FieldType,
a.length as CharLength,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as FieldLength,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as DecimalDigits,
(case when a.isnullable=1 then '1'else '0' end) IsNull,
isnull(g.[value],'') AS Description ,
isnull(e.text,'') DefaultValue
                              
FROM  syscolumns a
left join systypes b on a.xtype=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 
where d.name ='UserInfo'
order by a.id,a.colorder