表结构查询 Sql

select row_number() over(order by a.column_id) rownumber, a.name, 
case
when t.name in (N'decimal', N'numeric') then t.name + '('+ Convert(varchar(10),a.precision) + ',' + Convert(varchar(10),a.scale) +')'
when t.name in (N'varchar', N'nvarchar', N'char', N'nchar') then t.name + '('+ Convert(varchar(10),a.max_length) +')'
else t.name end
data_type,
case a.is_nullable when 1 then 'Y' else 'N' end is_nullable,
case a.is_identity when 1 then 'Y' else '' end is_identity,
isnull(def.text,N'') defalut_value,
isnull(ft.name + '.' + fc.name,N'') foreig_key,
isnull(ep.value,N'') [description]
from sys.columns a
left join sys.types t on t.user_type_id = a.user_type_id
left join sys.syscolumns sc on sc.id = a.object_id and sc.colid = a.column_id
left join sys.syscomments def on def.id = sc.cdefault
left join sys.foreign_key_columns fkc on fkc.parent_object_id = a.object_id and fkc.parent_column_id = a.column_id
left join sys.columns fc on fc.object_id = fkc.referenced_object_id and fc.column_id = fkc.referenced_column_id
left join sys.tables ft on ft.object_id = fkc.referenced_object_id
left join sys.extended_properties ep on ep.major_id = a.object_id and ep.minor_id = a.column_id and ep.name = N'MS_Description'
where exists(select 1 from sys.tables b where b.object_id = a.object_id and b.name = N'表名称')
原文地址:https://www.cnblogs.com/itstac/p/10819822.html