1. 程式人生 > >PowerDesigner 15進行逆向工程生成資料庫圖表時,註釋的comment的生成,解決PowerDesigner逆向工程沒有列註釋

PowerDesigner 15進行逆向工程生成資料庫圖表時,註釋的comment的生成,解決PowerDesigner逆向工程沒有列註釋

 使用PowerDesigner預設配置逆向工程是沒有註釋(name列為英文,comment列是空的),這樣的不方便檢視欄位具體是什麼意義,將註釋一同匯出,方便檢視欄位具體的意義,如下圖

 

 註釋列匯出步驟

1、新建指令碼DBMS,選擇選單:Tools→Resources→DBMS

彈出List Of DBMS對話方塊,選擇新建

彈出新建對話方塊,填寫名稱為“My Microsoft SQL Server 2008”(這個名稱可以自己定義),選擇“Microsoft SQL Server 2008”,點選OK

 

上圖預設檔案,不需要修改,直接點儲存,彈出下圖

 點選展開script->objects->column,選中SqlListQuery,如下圖

 

將下面的指令碼複製到右邊的Value輸入框中覆蓋原來的指令碼,點選確定

{OWNER, TABLE, S, COLUMN, DTTPCODE, LENGTH, SIZE, PREC, COMPUTE, NOTNULL, IDENTITY, DOMAIN, DEFAULT, ExtIdentitySeedInc,COMMENT,COLNNAME, 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, convert(varchar(8000), e.value) as colnE, convert(varchar(8000), e.value) as colnF, 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 c join [%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) left outer join [%CATALOG%.]sys.extended_properties e on (e.class=u.schema_id and e.major_id=o.object_id and e.minor_id = c.column_id and e.name=N'MS_Description') where o.type in ('U', 'S', 'V') [ and u.name = %.q:OWNER%] [ and o.name=%.q:TABLE%] order by 1, 2, 3
View Code

選擇File→Reverse Engineer→Database 對資料庫進行逆向工程,DBMS選擇剛才新建的“My Microsoft SQL Server 2008”,點選確定

選擇使用剛才新建的“My Microsoft SQL Server 2008”

選項按照下圖選中的,點選確定

到此修改配置完成,下面開始生產逆向工程

按照下圖步驟操作

 

 

點選確定

選擇資料庫點選OK

 

點選OK就可以生產註釋的逆向工程

上面步驟資料庫表的註釋comment沒有匯出來,下面匯出資料庫表註釋comment

選擇選單:工具→Execute Commands→Edit/Run Script,Run執行vbs指令碼即可。

將下面的指令碼複製到上圖的輸入框中,點選run,就可生成表的comment註釋了

'把pd中那麼name想自動新增到comment裡面
'如果comment為空,則填入name;如果不為空,則保留不變,這樣可以避免已有的註釋丟失.
 
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 comment 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 
    if trim(tab.comment)="" then '如果有表的註釋,則不改變它.如果沒有表註釋.則把name新增到註釋裡面. 
       tab.comment = tab.name 
    end if  
 Dim col ' running column  
 for each col in tab.columns 
  if trim(col.comment)="" then '如果col的comment為空,則填入name,如果已有註釋,則不新增;這樣可以避免已有註釋丟失.
   col.comment= col.name 
  end if 
 next  
  end if  
 next  
   
 Dim view 'running view  
 for each view in folder.Views  
  if not view.isShortcut and trim(view.comment)=""  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
View Code

點選run,匯出的表註釋效果如下

 

 

PowerDesigner 15的下載連結: https://pan.baidu.com/s/1ryohBB9RSvKbzxjjaCs69g 提取碼: r54q