Sql Server 导出数据库表结构的SQL查询语句
1 --导出数据库所有表 2 3 SELECT 4 表名 = Case When A.colorder=1 Then D.name Else '' End, 5 表说明 = Case When A.colorder=1 Then isnull(F.value,'') Else '' End, 6 字段序号 = A.colorder, 7 字段名 = A.name, 8 字段说明 = isnull(G.[value],''), 9 标识 = Case When COLUMNPROPERTY( A.id,A.name,'IsIdentity')=1 Then '√'Else '' End, 10 主键 = Case When exists(SELECT 1 FROM sysobjects Where xtype='PK' and parent_obj=A.id and name in ( 11 SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = A.id AND colid=A.colid))) then '√' else '' end, 12 类型 = B.name, 13 占用字节数 = A.Length, 14 长度 = COLUMNPROPERTY(A.id,A.name,'PRECISION'), 15 小数位数 = isnull(COLUMNPROPERTY(A.id,A.name,'Scale'),0), 16 允许空 = Case When A.isnullable=1 Then '√'Else '' End, 17 默认值 = isnull(E.Text,'') 18 FROM 19 syscolumns A 20 Left Join 21 systypes B 22 On 23 A.xusertype=B.xusertype 24 Inner Join 25 sysobjects D 26 On 27 A.id=D.id and D.xtype='U' and D.name<>'dtproperties' 28 Left Join 29 syscomments E 30 on 31 A.cdefault=E.id 32 Left Join 33 sys.extended_properties G 34 on 35 A.id=G.major_id and A.colid=G.minor_id 36 Left Join 37 38 sys.extended_properties F 39 On 40 D.id=F.major_id and F.minor_id=0 41 --where d.name='OrderInfo' --如果只查询指定表,加上此条件 42 Order By 43 A.id,A.colorder