在做个小项目,需要导出表的字段信息,下面是针对,ms sql导出表字段信息的sql语句:
sql 2000:
Code
1SELECT
2
3 (case when a.colorder=1 then d.name else '' end)表名,
4
5 a.colorder 字段序号,
6
7 a.name 字段名,
8
9 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
10
11 (case when (SELECT count(*)
12
13 FROM sysobjects
14
15 WHERE (name in
16
17 (SELECT name
18
19 FROM sysindexes
20
21 WHERE (id = a.id) AND (indid in
22
23 (SELECT indid
24
25 FROM sysindexkeys
26
27 WHERE (id = a.id) AND (colid in
28
29 (SELECT colid
30
31 FROM syscolumns
32
33 WHERE (id = a.id) AND (name = a.name))))))) AND
34
35 (xtype = 'PK'))>0 then '√' else '' end) 主键,
36
37 b.name 类型,
38
39 a.length 占用字节数,
40
41 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
42
43 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,
44
45 (case when a.isnullable=1 then '√'else '' end) 允许空,
46
47 isnull(e.text,'') 默认值,
48
49 isnull(g.[value],'') AS 字段说明
50
51
52FROM syscolumns a left join systypes b
53
54on a.xtype=b.xusertype
55
56inner join sysobjects d
57
58on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
59
60left join syscomments e
61
62on a.cdefault=e.id
63
64left join sysproperties g
65
66on a.id=g.id AND a.colid = g.smallid
67
68order by a.id,a.colorder
69
70
71
72
1SELECT
2
3 (case when a.colorder=1 then d.name else '' end)表名,
4
5 a.colorder 字段序号,
6
7 a.name 字段名,
8
9 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
10
11 (case when (SELECT count(*)
12
13 FROM sysobjects
14
15 WHERE (name in
16
17 (SELECT name
18
19 FROM sysindexes
20
21 WHERE (id = a.id) AND (indid in
22
23 (SELECT indid
24
25 FROM sysindexkeys
26
27 WHERE (id = a.id) AND (colid in
28
29 (SELECT colid
30
31 FROM syscolumns
32
33 WHERE (id = a.id) AND (name = a.name))))))) AND
34
35 (xtype = 'PK'))>0 then '√' else '' end) 主键,
36
37 b.name 类型,
38
39 a.length 占用字节数,
40
41 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
42
43 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,
44
45 (case when a.isnullable=1 then '√'else '' end) 允许空,
46
47 isnull(e.text,'') 默认值,
48
49 isnull(g.[value],'') AS 字段说明
50
51
52FROM syscolumns a left join systypes b
53
54on a.xtype=b.xusertype
55
56inner join sysobjects d
57
58on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
59
60left join syscomments e
61
62on a.cdefault=e.id
63
64left join sysproperties g
65
66on a.id=g.id AND a.colid = g.smallid
67
68order by a.id,a.colorder
69
70
71
72
sql 2oo5:
Code
1SELECT
2 (case when a.colorder=1 then d.name else '' end) 表名,
3 a.colorder 字段序号,
4 a.name 字段名,
5 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
6 (case when (SELECT count(*)
7 FROM sysobjects
8 WHERE (name in
9 (SELECT name
10 FROM sysindexes
11 WHERE (id = a.id) AND (indid in
12 (SELECT indid
13 FROM sysindexkeys
14 WHERE (id = a.id) AND (colid in
15 (SELECT colid
16 FROM syscolumns
17 WHERE (id = a.id) AND (name = a.name))))))) AND
18 (xtype = 'PK'))>0 then '√' else '' end) 主键,
19 b.name 类型,
20 a.length 占用字节数,
21 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
22 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,
23 (case when a.isnullable=1 then '√'else '' end) 允许空,
24 isnull(e.text,'') 默认值,
25 g.[value] AS 字段说明
26
27FROM syscolumns a left join systypes b
28on a.xtype=b.xusertype
29inner join sysobjects d
30on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
31left join syscomments e
32on a.cdefault=e.id
33left join sys.extended_properties g
34on a.id=g.major_id AND a.colid = g.minor_id
35--WHERE d.[name]='AgentMessage' --你要查看的表名,注释掉,查看当前数据库所有表的字段信息
36order by a.id,a.colorder
37
38
1SELECT
2 (case when a.colorder=1 then d.name else '' end) 表名,
3 a.colorder 字段序号,
4 a.name 字段名,
5 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
6 (case when (SELECT count(*)
7 FROM sysobjects
8 WHERE (name in
9 (SELECT name
10 FROM sysindexes
11 WHERE (id = a.id) AND (indid in
12 (SELECT indid
13 FROM sysindexkeys
14 WHERE (id = a.id) AND (colid in
15 (SELECT colid
16 FROM syscolumns
17 WHERE (id = a.id) AND (name = a.name))))))) AND
18 (xtype = 'PK'))>0 then '√' else '' end) 主键,
19 b.name 类型,
20 a.length 占用字节数,
21 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
22 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,
23 (case when a.isnullable=1 then '√'else '' end) 允许空,
24 isnull(e.text,'') 默认值,
25 g.[value] AS 字段说明
26
27FROM syscolumns a left join systypes b
28on a.xtype=b.xusertype
29inner join sysobjects d
30on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
31left join syscomments e
32on a.cdefault=e.id
33left join sys.extended_properties g
34on a.id=g.major_id AND a.colid = g.minor_id
35--WHERE d.[name]='AgentMessage' --你要查看的表名,注释掉,查看当前数据库所有表的字段信息
36order by a.id,a.colorder
37
38
copy结果到excel或word,就ok了。相当于数据字典了。
参考:
http://blog.csdn.net/ITOfficeboy/archive/2009/03/04/3955400.aspx