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

sqlserver导出数据库表结构的操作教程

程序员文章站 2022-03-26 21:53:05
sqlserver导出表结构的操作教程 select 表名称 = case when columns.colorder=1 then objects.name else ''...

sqlserver导出表结构的操作教程

select
  表名称 = case when columns.colorder=1 then objects.name else '' end,
  表说明 = case when columns.colorder=1 then isnull(properties2.value,'') else '' end,
  列名称 = columns.name,
  列说明 = isnull(properties1.[value],''),
  类型 = types.name,
  长度 = columnproperty(columns.id,columns.name,'precision'),
  是否是主键 = 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 = columns.id and colid=columns.colid 
  ))) then '是' else '否' end,
  默认值=isnull(comments.text,''),
  是否允许为空=case when columns.isnullable=1 then '是'else '否' end
  from syscolumns columns
  left join systypes types on columns.xtype=types.xusertype 
  inner join sysobjects objects on columns.id=objects.id and objects.xtype='u' and objects.name<>'dtproperties' 
  left join syscomments comments on columns.cdefault=comments.id 
  left join sys.extended_properties properties1 on columns.id=properties1.major_id and columns.colid=properties1.minor_id  
  left join sys.extended_properties properties2 on objects.id=properties2.major_id and properties2.minor_id =0 
  --where d.name='' --查询指定表
  order by columns.id,columns.colorder