导出表结构

数据库迁移时,导出表结构,用设计工具导入

1、

SELECT  
字段名=a.name, 
名称=a.name, 
类型=CASE WHEN b.name='int'  THEN '整数' WHEN  b.name='ntext' THEN '文本'WHEN  b.name='date' THEN '日期'
	WHEN  b.name='smalldatetime' THEN '日期'WHEN  b.name='bigint' THEN '长整型'ELSE'字符' END, 
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'), 
允许空='',--case when a.isnullable=1 then '√'else '' end, 
默认值=isnull(e.text,''), 
字段说明=isnull(g.[value],'') ,
b.name,
主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
  SELECT name FROM sysindexes WHERE indid in(
   SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid 
   ))) then '√' else '' END,
标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '√'else '' end
FROM syscolumns a 
left join systypes b on a.xtype=b.xusertype 
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' 
left join syscomments e on a.cdefault=e.id 
left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id 
left join sys.extended_properties f on d.id=f.major_id and f.minor_id =0 
where d.name='CMS_Channels' --如果只查询指定表,加上此条件   
order by a.id,a.colorder

结果如下

导出表结构_第1张图片

2、导入

 

你可能感兴趣的:(SqlServer)