欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

表结构查询 Sql

程序员文章站 2022-12-05 13:59:08
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),... ......
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'表名称')