sqlserver 查询表结构字段属性

根子 sqlserver 2022-02-14 11:24 原创 742


SELECT
    序号       = a.colorder,
    字段名称   = a.name,    
    字段类型   = c.name,
    字段长度   = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
    允许为空   = case when a.isnullable=1 then '√'else '' end,
	标识       = case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '√'else '' end,
    主键       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in 
	(SELECT distinct name FROM sysindexes WHERE indid in(SELECT distinct indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
    默认值     = isnull(b.text,''),
    字段说明   = isnull(e.[value],'')
FROM syscolumns a
left join syscomments b on a.cdefault=b.id
join systypes c on a.xusertype=c.xusertype
join sysobjects d on a.id=d.id and d.xtype='U'
left join sys.extended_properties e on a.id=e.major_id and a.colid=e.minor_id
where d.name='Departments' order by a.colorder


留 言

木艮子の笔记