加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

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

发布时间:2018-08-18 08:30:27 所属栏目:站长百科 来源:站长网
导读:'***************************************************************************** '文件:SetIdentity.vbs '版本: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

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读