自动设置Identity属性的代码(PowerDesigner脚本)

发表于:2007-07-02来源:作者:点击数: 标签:
@#***************************************************************************** @#文件:SetIdentity. vb s @#版本:1.0 @#版权:floodzhu (floodzhu@hotmail.com),2004.12.31 @#功能:遍历物理模型中的所有表,把是主键但不是外键的字段设置为Identity
@#*****************************************************************************
@#文件:SetIdentity.vbs
@#版本:1.0
@#版权:floodzhu (floodzhu@hotmail.com),2004.12.31
@#功能:遍历物理模型中的所有表,把是主键但不是外键的字段设置为Identity,适用于
@#      物理模型为MS Sql Server的类型。
@#用法:打开物理模型,运行本脚本(Ctrl+Shift+X)
@#备注:我有两个习惯,一个是把所有表的主键都定义为自增长的int类型,另一个是定义
@#      一个Domain叫ID,在设计概念模型时把所有的PrimaryKey字段的Domain设置为ID
@#      类型。
@#
@#      如果我进行了上面的设置,则在转化为物理模型时需要手工设置Identity,
@#      最笨的方法是一个表一个表进行设置,最简单的方法是在物理模型中直接对Domain
@#      进行设置。对Domain进行设置有一个小缺点,就是如果该字段不是主键也不是生
@#      成的外键,而是一个一般字段,例如表示树状结构的PID,则它也会被设置为
@#      Identity,不过由于这种字段比较少,而且在生成数据库时会发生错误可以提醒
@#      你进行纠正,所以可以轻松过关而不至于隐藏错误,所以是一种好方法。
@#
@#      用下面的代码可以给你第三种选择,而不会发生任何错误。
@#*****************************************************************************
dim model @#current model
set model = ActiveModel

If (model Is Nothing) Then
   MsgBox "There is no current Model"
ElseIf Not model.IsKindOf(PdPDM.cls_Model) Then
   MsgBox "The current model is not an Physical Data model."
Else
   ProcessTables model
End If

@#*****************************************************************************
@#函数:ProcessTables
@#功能:递归遍历所有的表
@#*****************************************************************************
sub ProcessTables(folder)
   @#处理模型中的表
   dim table
   for each table in folder.tables
      if not table.IsShortCut then
         ProcessTable table
      end if
   next
  
   @#对子目录进行递归
   dim subFolder
   for each subFolder in folder.Packages
      ProcessTables subFolder
   next
end sub

@#*****************************************************************************
@#函数:ProcessTable
@#功能:遍历指定table的所有字段,如果该字段是主键但不是外键,则设置为Identity
@#*****************************************************************************
sub ProcessTable(table)
   dim col
   for each col in table.Columns
      @#对于是主键且不是外键的字段设置为Identity(自增长类型)
      if col.Primary and not col.ForeignKey then
         col.Identity = true
      end if
   next
end sub

原文转自:http://www.ltesting.net