PowerDesigner-VBSrcipt-自动设置主键,外键名等(SQL Server)

在PowerDesigner中的设计SQL Server 数据表时,要求通过vbScript脚本实现下面的功能:

 

主键:pk_TableName

外键:fk_TableName_ForeignKeyColumnList

 

当字段作为主键,而且类型为smallint,int,bigint,那么要设置Identity =true.

当字段作为主键,而且类型为uniqueidentifier,那么要设置默认值为newid(),而且设置扩展属性rowguidcol.

 


复制代码
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
Dim mdl ' the current 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

Private sub ProcessFolder(folder)
    'Tables
   Dim tab
   for each tab in folder.tables
       
       dim col
       for each col in tab.columns 
            
            '自動設置Identity
            if col.primary =true and (col.datatype ="smallint" or col.datatype = "int"  or col.datatype = "bigint") then
               col.identity=true
               
            '自動設置ROWGUIDCOL   
            elseif col.primary=true and col.datatype="uniqueidentifier" then
               col.DefaultValueDisplayed="newid()"   
               col.SetExtendedAttributeText "ExtRowGUIDCol",true
            end if
            
       next    
       
      '自動設置主鍵
      dim ky
      for each ky in tab.Keys
            if ky.primary =true then
               ky.Name="pk_"+tab.Name
               ky.Code=ky.Name
               ky.ConstraintName=ky.Name
               ky.Clustered=true
            end if
      next
                       
   next
   
   '自動設置外鍵
   dim ref
   for each ref in folder.References
      ref.name="fk_"+ref.ChildTable.Name +"_"+ref.ForeignKeyColumnList
      ref.Code=ref.Name
      ref.ForeignKeyConstraintName=ref.name
   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
复制代码

 

 

posted @   ok_008  阅读(1816)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
给我写信
点击右上角即可分享
微信分享提示