PowerDesigner批量添加索引、修改索引
使用方式:
打开需要操作的pdm,然后按快捷键:ctrl + shift +x
拷贝以下代码,点击Run(F5), 开始递归目录,稍等片刻弹出提示及完成操作。
命名格式:
索引:IDX_TableName_序号
'******************************************************************************
'* File: Add_Index.vbs
'* Purpose: 为主键添加索引,或修改索引名称,ctrl+shift+x 粘贴代码点击运行
'* Title: 索引格式: IDX_<表名>_序号
'* Category:
'* Version: 1.0
'* Company: Sybase Inc.
'******************************************************************************
Option Explicit
'
' get the current active model
'
Dim mdl ' the current model
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
End If
Dim fldr,line
Set Fldr = ActiveDiagram.Parent
ListObjects(fldr)
MsgBox "处理完成,请在PDM中查看"
Private Sub ListObjects(fldr)
Dim tab,tab2 ' running object
For Each tab In fldr.tables
output "Found "+tab.ClassName+" "+tab.Name
'删除以前的所有索引
Dim inx
for each inx in tab.indexes
inx.Delete()
next
line = 1
'遍历每一行,如果是主键就添加索引
Dim col,col2
for each col in tab.columns
if col.primary =true then
'为主键添加索引
AddIndex tab,col,line
line = line + 1
else
'为外键添加索引,没有建立外键关联,只是字段名称相同
For Each tab2 In fldr.tables
for each col2 in tab2.columns
if col2.primary =true and col2.Code=col.Code then
AddIndex tab,col,line
line = line + 1
end if
Next
Next
end if
next
Next
End Sub
'
' 为表的某一列增加索引
'
Private Sub AddIndex(table,column,line)
Dim newIndex,newIndexCol
set newIndex = table.Indexes.CreateNew()
set newIndexCol = newIndex.IndexColumns.CreateNew()
newIndexCol.column = column
newIndex.Name = "IDX_"&table.Code&"_"&Cstr(line)
newIndex.Code = newIndex.Name
End Sub
'
' 统一修改索引名字
'
Private Sub ChangeIndexName(table)
Dim inx,line
line = 1
for each inx in table.Indexes
inx.Name= "IDX_"&table.Code&"_"&Cstr(line)
inx.Code= inx.Name
line = line + 1
next
End Sub