SqlServer 查询出列的一些基本信息,表名,列名,数据类型,备注说明,最大宽度,递增,是否为空,是否主键

SELECT b.name as TableName,a.name ColumnName,c.name as DataType,p.value as Description,a.max_length as MaxLength
,(case a.is_identity when 1 then 'True' else 'False' end) as [Identity]
,(case a.is_nullable when 1 then 'True' else 'False' end) as [IsNull]
,(case k.type when 'PK' then 'True' else 'False' end) as [IsKey]
FROM sys.objects b
INNER JOIN sys.columns a ON a.object_id = b.object_id
LEFT JOIN sys.types c ON a.system_type_id=c.user_type_id
LEFT JOIN sys.extended_properties p ON p.major_id = a.object_id AND p.minor_id = a.column_id and p.name = 'MS_Description'
left join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE scc on scc.TABLE_NAME=b.name and scc.COLUMN_NAME=a.name
left join sys.objects k on k.name=scc.CONSTRAINT_NAME and k.type='PK'
WHERE b.type='U' and a.name='ID'

原文地址:https://www.cnblogs.com/shaoming01/p/2987225.html