---
title: "Powerdesigner逆向工程生成PDM时的列注释(ColumnComment)解决方案 "
author: "lanxing"
date: "2012-02-20T15:52:25Z"
tags: []
categories: ["数据库"]
slug: "powerdesigner-pdm-columncomment"
draft: false
lastmod: "2012-02-20T15:52:25Z"
id: 1413657
url: "https://lanxing.iteye.com/blog/1413657"
---

在用powerdesigner逆向工程生成PDM时，列注释(ColumnComment)始终无法生成，历经数小时的探索，找到一个折衷的方法，现分享如下。并希望有高手指点更好的方法。

邀月使用的是Powerdesigner 15.2，数据库为SQL Server 2008 r2

方法如下：

1、在PowerDesigner界面－File-Reverse Engineer－Database,然后选择一个DBMS,我这里选取一个由系统默认的SQL Server 2008 DBMS修改而来的SQL\_2008\_MyDefine。

![邀月工作室](http://pdqlrg.blu.livefilestore.com/y1p_7nuwhAWNSwnjM3Gfa993grfAd9aMxEshWI5OXMJYqwapphtiCrQzoAUl_FpIquTug_YgWGVbKaMQZVUc3FW9Zx_tq00ZqFM/2011-3-4%2015-17-44.png?psid=1 "邀月工作室")

选好odbc，输入正确的连接串，选择数据库及表。

![邀月工作室](http://public.blu.livefilestore.com/y1pTZQt0qJNOmqxAaDlc6hCpHG_v3wl64SpYdnsv7oF5DFI56OdUytaOIhukY1iV2kSeX0a5LTsPz0pTQ3KQ0dWWQ/2011-3-4%2015-18-44.png?psid=1 "邀月工作室")

生成效果：Name为英文，且Comment列为空。

![邀月工作室](http://public.blu.livefilestore.com/y1p8Il5EMuJ5e3ELd2sD6T8knaFBdzmxLTt-6X6w9_kz-XkYQFS069e--gcZhZ3sdc0b0Sydd7QkOSUewl2pfOEVg/2011-3-4%2015-20-34.png?psid=1 "邀月工作室")

而我们希望的是Name列为中文，Comment为中文。

查看了下在线帮助，发现在SQL\_2008\_MyDefine::Script\\Objects\\Column\\SqlListQuery下的Value值如下：

 ![邀月工作室](http://public.blu.livefilestore.com/y1pWJb6CwljEQtYyxPw8Eo80fhc2BacriuS4utvS9GWW2fPZbzgptxJJX6xWl6lI-TcqXUsvNV2_63022xNIHlNHg/2011-3-4%2015-25-27.png?psid=1 "邀月工作室")

![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)View Code

```


{OWNER, TABLE, S, COLUMN, DTTPCODE, LENGTH, SIZE, PREC, COMPUTE, NOTNULL, IDENTITY, DOMAIN, DEFAULT, ExtIdentitySeedInc, COMMENT, ExtCollation, ExtIdtNotForReplication, ExtDeftConstName, Sparse, FileStream, ExtRowGuidCol}select u.name,
o.name,
c.column_id,
c.name,case when c.system_type_id in (165, 167, 231) and c.max_length = -1 then t.name + '(Max)' else t.name end,
c.precision,case (c.max_length) when -1 then 0 else case when c.system_type_id in (99, 231, 239) then (c.max_length/2) else (c.max_length) end end as colnA,
c.scale,case(c.is_computed) when 1 then convert(varchar(8000), (select z.definition from [%CATALOG%.]sys.computed_columns z where z.object_id = c.object_id and z.column_id = c.column_id)) else '' end as colnB,case(c.is_nullable) when 1 then 'NULL' else 'NOTNULL' end,case(c.is_identity) when 1 then 'identity' else '' end,case when(c.user_type_id <> c.system_type_id) then (select d.name from [%CATALOG%.]sys.types d where d.user_type_id = c.user_type_id) else '' end as colnC,convert(varchar(8000), d.definition),case (c.is_identity) when 1 then convert(varchar, i.seed_value) + ', ' + convert(varchar, i.increment_value) else '' end as colnD,
(select convert(varchar(8000), value) from ::fn_listextendedproperty(NULL, 'user', u.name, 'table', o.name, 'column', c.name) where name = 'MS_Description') as colnE,
c.collation_name,case (i.is_not_for_replication) when 1 then 'true' else 'false' end,
d.name,case(c.is_sparse) when 1 then 'true' else 'false' end,case(c.is_filestream) when 1 then 'true' else 'false' end,case(c.is_rowguidcol) when 1 then 'true' else 'false' end from [%CATALOG%.]sys.columns cjoin [%CATALOG%.]sys.objects o on (o.object_id = c.object_id)join [%CATALOG%.]sys.schemas u on (u.schema_id = o.schema_id)join [%CATALOG%.]sys.types t on (t.user_type_id = c.system_type_id)left outer join [%CATALOG%.]sys.identity_columns i on (i.object_id = c.object_id and i.column_id = c.column_id)left outer join [%CATALOG%.]sys.default_constraints d on (d.object_id = c.default_object_id)where o.type in ('U', 'S', 'V')[ and u.name = %.q:OWNER%] [ and o.name=%.q:TABLE%] order by 1, 2, 3

```

注意该值中上面是PDM对应的列名，下面是从SQL数据库中取到的相关属性值。其中“

```


(select convert(varchar(8000), value) from 
::fn_listextendedproperty(NULL, 'user', u.name, 'table', o.name, 'column', c.name) where name = 'MS_Description') as colnE, 

```

”是可以取到Comment值的，可是生成的PDM为什么Comment列为空呢？尝试修改该SQL语句，将语句提取出来，也可以获取结果：

![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)View Code

```


select u.name,
o.name,
c.column_id,
c.name,case when c.system_type_id in (165, 167, 231) and c.max_length = -1 then t.name + '(Max)' else t.name end,
c.precision,case (c.max_length) when -1 then 0 else case when c.system_type_id in (99, 231, 239) then (c.max_length/2) else (c.max_length) end end as colnA,
c.scale,case(c.is_computed) when 1 then convert(varchar(8000), (select z.definition from sys.computed_columns z where z.object_id = c.object_id and z.column_id = c.column_id)) else '' end as colnB,case(c.is_nullable) when 1 then 'NULL' else 'NOTNULL' end,case(c.is_identity) when 1 then 'identity' else '' end,case when(c.user_type_id <> c.system_type_id) then (select d.name from sys.types d where d.user_type_id = c.user_type_id) else '' end as colnC,convert(varchar(8000), d.definition),case (c.is_identity) when 1 then convert(varchar, i.seed_value) + ', ' + convert(varchar, i.increment_value) else '' end as colnD,
(select convert(varchar(8000), value) from ::fn_listextendedproperty(NULL, 'user', u.name, 'table', o.name, 'column', c.name) where name = 'MS_Description') as colnE,
c.collation_name,case (i.is_not_for_replication) when 1 then 'true' else 'false' end,
d.name,case(c.is_sparse) when 1 then 'true' else 'false' end,case(c.is_filestream) when 1 then 'true' else 'false' end,case(c.is_rowguidcol) when 1 then 'true' else 'false' end from sys.columns cjoin sys.objects o on (o.object_id = c.object_id)join sys.schemas u on (u.schema_id = o.schema_id)join sys.types t on (t.user_type_id = c.system_type_id)left outer join sys.identity_columns i on (i.object_id = c.object_id and i.column_id = c.column_id)left outer join sys.default_constraints d on (d.object_id = c.default_object_id)where o.type in ('U', 'S', 'V')and u.name =N'dbo' and o.name=N'Age' order by 1, 2, 3

```

后来发现，直接用生成表的SQL，而不是用数据库就可以生成注释。如下图：

![邀月工作室](http://public.blu.livefilestore.com/y1pSjiDL4ygG_5IzhHwHCtTHLYXUAmS5MHbf76ixsWBvypyQNGH3qNCYG5CJgxgsuI63aPM4fqpzEUbGfrdhatH4A/2011-3-4%2015-32-14.png?psid=1 "邀月工作室")

但字段类型全乱了。

![邀月工作室](http://public.blu.livefilestore.com/y1pCUNAvpw-ktgJr8sQuXDbzZuuMe-dXSkZqa8tisZiT0ONmuDR8mmD81cN0y6Ahh_M4mhrfeRZ1Yv84F2fMXk8IA/2011-3-4%2016-57-10.png?psid=1 "邀月工作室")

难道Powerdesigner真的这么弱吗？google了一下，看到官方文档：

[http://manuals.sybase.com/onlinebooks/group-pd/pdd1100e/advanced/@Generic\_\_BookTextView/1302;hf=0](http://manuals.sybase.com/onlinebooks/group-pd/pdd1100e/advanced/@Generic__BookTextView/1302;hf=0 "http://manuals.sybase.com/onlinebooks/group-pd/pdd1100e/advanced/@Generic__BookTextView/1302;hf=0")

里面有关于SqlListQuery的解释。

再看看上面的SQL语句，突然想到，既然可以在SQL查询出结果，那么出错一定在生成PDM的时候，

在SQL\_2008\_MyDefine::Script\\Objects\\Column中看到这么一段:

```


The following system variables are available:
(parent table items are also available for columns)
   "COLNNAME"     // name of the column    "COLNCODE"     // code of the column

```

将上面的Value值中上面的一行中“Comment”修改为“COLNNAME”会如何？即

{OWNER, TABLE, S, COLUMN, DTTPCODE, LENGTH, SIZE, PREC, COMPUTE, NOTNULL, IDENTITY, DOMAIN, DEFAULT, ExtIdentitySeedInc, COMMENT, ExtCollation, ExtIdtNotForReplication, ExtDeftConstName, Sparse, FileStream, ExtRowGuidCol}改为

{OWNER, TABLE, S, COLUMN, DTTPCODE, LENGTH, SIZE, PREC, COMPUTE, NOTNULL, IDENTITY, DOMAIN, DEFAULT, ExtIdentitySeedInc, COLNNAME, ExtCollation, ExtIdtNotForReplication, ExtDeftConstName, Sparse, FileStream, ExtRowGuidCol}

结果生成如下：

![邀月工作室](http://public.blu.livefilestore.com/y1pCUNAvpw-kti33a6Gcm6Qs3cIUDDOEZnwP7Rm7yu9uSKZtU0HmS2jMOfQ80yo78udk4BWnks2hd6OJTfiurubhA/2011-3-4%2020-25-45.png?psid=1 "邀月工作室")

同理将SQL\_2008\_MyDefine::Script\\Objects\\Table\\SqlListQuery下的Value的第一行中

{OWNER, TABLE, TABLE\_TYPE, Comment}的Comment改为{OWNER, TABLE, TABLE\_TYPE, TNAME}

生成结果，如下：

 ![邀月工作室](http://public.blu.livefilestore.com/y1phWtnvvqaYNDelznjFRhhAhHJrBxzgTc6tTs28Xjt3KEvd6vdHSFdhMWO1gcN_1ug3X_PJAj2xEJ2mH1j9GJqYA/2011-3-4%2020-32-43.png?psid=1 "邀月工作室")

至此，我的基本目的，已经达到了，虽然没有直接生成Comment列，但可以通过以下vbs脚本，从Name列生成Comment列：

'代码一:将name生成comment的脚本

![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)View Code

```


Option Explicit                          
ValidationMode = True           
InteractiveMode = im_Batch  Dim mdl 'the current model    'get the current active model  Set mdl = ActiveModel           If (mdl Is Nothing) Then          MsgBox "There is no current Model"                                                                                           ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then                                                                      MsgBox "The current model is not an Physical Data model."                                                   Else                                                                                                                                                  
ProcessFolder mdl                                                                                                                         End If                                                                                                                                               'This routine copy name into code for each table, each column and each view                      'of the current folder                                                                                                                      Private sub ProcessFolder(folder)                                                                                                Dim Tab 'running table                                                                                                                  for each Tab in folder.tables                                                                                                         if not tab.isShortcut then                                                                                                               
tab.comment = tab.name                                                                                                              Dim col 'running column                                                                                                               for each col in tab.columns                                                                                                           
col.comment= col.name                                                                                                                next                                                                                                                                                  end if                                                                                                                                               next                                                                                                                                                  Dim view 'running view                                                                                                                 for each view in folder.Views                                                                                                        if not view.isShortcut then                                                                                                            
view.comment = view.name                                                                                                         end if                                                                                                                                               next                                                                                                                                                  'go into the sub-packages                                                                                                              Dim f 'running folder                                                                                                                      For Each f In folder.Packages                                                                                                        if not f.IsShortcut then                                                                                                                   
ProcessFolder f                                                                                                                               end if                                                                                                                                               Next                                                                                                                                                 end sub         

```

仔细思索：为什么从SQL生成的脚本有Comment,可是到生成PDM时为什么没有转化成功呢？是否Comment名称不对应？

于是将原来的COMMENT加上COLNNAME，同时在下面的SQL语句中增加一列与COMMENT相同的值：

(select convert(varchar(8000), value) from ::fn\_listextendedproperty(NULL, 'user', u.name, 'table', o.name, 'column', c.name) where name = 'MS\_Description') as colnF,

如下图：

![邀月工作室](http://public.blu.livefilestore.com/y1p0PMw_kYeUhfhx_7Uf8GXmqE9rWzONYM6p5ofLsfBFr70X0aZjuWrS9EO5sLaII7ytMAkS4CUr-91PD6CPlkwkw/2011-3-4%2021-01-01.png?psid=1 "邀月工作室")

这样，Name列和Comment同时生成，终于OK！

![邀月工作室](http://public.blu.livefilestore.com/y1pjVhDSKKk-0hFGbFaFMAKkvBBL3T1W7PLgl8TudzYRcT6MhtLEi5KodwPwOPJ88Q11e1dcvZS2KdOkozotTPBZg/2011-3-4%2021-03-35.png?psid=1 "邀月工作室")

**邀月注：本文版权由邀月和博客园共同所有，转载请注明出处。**