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

sqlserver 导出数据库表结构

程序员文章站 2022-03-15 15:02:56
...
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 
转载至:https://www.cnblogs.com/miaomiaoquanfa/p/6909835.html