SQL2005系统升级手记之四-自动生成数据字典与系统表Sys.extended_properites
在SQL Server 2000 中,好多人都喜欢在表和列的扩展属性里面添加说明,然后用脚本生成数据库字典,升级到SQL Server 2005 后,会发现SQL Server 2000上的脚本不能使用了,其中绝大多数都是因为SQL Server 2005已经废弃了系统表sysproperties。
注意,sysproperties因为是无正式文档记载的系统表,已经在Sql2005中废弃了,Sql2005中使用Sys.extended_properites保存用户自定义的表、字段的属性信息。
原先的脚本:
改为
注意,sysproperties因为是无正式文档记载的系统表,已经在Sql2005中废弃了,Sql2005中使用Sys.extended_properites保存用户自定义的表、字段的属性信息。
原先的脚本:
1 SELECT
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 olid
16 FROM yscolumns
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 isnull(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 .name<>'dtproperties'
31left join syscomments e
32on a.cdefault=e.id
33left join sysproperties g
34on a.id=g.id AND a.colid = g.smallid
35order by a.id,a.colorder
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 olid
16 FROM yscolumns
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 isnull(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 .name<>'dtproperties'
31left join syscomments e
32on a.cdefault=e.id
33left join sysproperties g
34on a.id=g.id AND a.colid = g.smallid
35order by a.id,a.colorder
改为
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 isnull(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.major_id
35order by a.id,a.colorder
36
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 isnull(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.major_id
35order by a.id,a.colorder
36