查询 数据库某表的所有字段信息

MsSQL:

select b.colorder as 序号, b.name AS 字段名,isnull(c.value,'') AS 注释,case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=b.id and name in (SELECT name FROM sysindexes WHERE indid in(SELECT indid FROM sysindexkeys WHERE id=b.id AND colid=b.colid))) then '√' else '' end as 是否主键,d.name as 数据类型,COLUMNPROPERTY(b.id,b.name,'PRECISION')as 长度,isnull(COLUMNPROPERTY(b.id,b.name,'Scale'),0)as 小数位数,case when b.isnullable=1 then '√'else '' end as 是否允许为空,isnull(e.text,'')as 默认值
from sysobjects a
INNER JOIN syscolumns b ON b.id=a.id
LEFT JOIN sys.extended_properties c ON c.major_id=b.id AND c.minor_id=b.colid
left join systypes d on b.xusertype=d.xusertype
left join syscomments e on b.cdefault=e.id
where a.name = '{tableName}'

MySQL:

Select column_name as 字段名,column_comment as 注释,data_type as 数据类型,case when is_nullable='YES' then '√' else '' end as 是否允许为空,column_default as 默认值,case when numeric_precision is not null then numeric_precision else character_maximum_length end as 长度,case when column_key='PRI' then '√' else '' end as 是否主键
from INFORMATION_SCHEMA.COLUMNS
Where table_name = '{tableName}'
AND table_schema = '{dbName}'

原文地址:https://www.cnblogs.com/Bingo1363136101/p/12842561.html