'Write by Stone 20110407
dim sIndexName
dim sIndexCode
dim sIndexCols
dim index_cols_code(20) '索引列名称
dim index_cols_name(20)
dim index_col_num '索引字段数
dim pk_name '主键名称
dim pk_code '主键编码
dim pk_constraint_name '约束名称
dim pk_obj 'PK对象
dim existpk
dim pk_col
dim index
for each TABLE in ActiveModel.tables
FOR each index in table.indexes
sIndexName = index.GetAttribute("Name") '索引Name的内容
sIndexCode = index.GetAttribute("Code") '索引Code的内容
if Right(sIndexName, 6) = "_UIDX1" then '目前暂以此为作为检查索引为唯一索引的依据
'读取索引字段内容
index_col_num = 0
sIndexCols = ""
for each indexcol in index.IndexColumns
index_cols_code(index_col_num) = indexcol.Code
index_cols_name(index_col_num) = indexcol.Name
index_col_num = index_col_num + 1
sIndexCols = sIndexCols + indexcol.Code + ","
next
'MsgBox sIndexName + "--"+ sIndexCols
'检查是否已PK
existpk = false
for each key in table.keys
if key.Primary = true then
existpk = true
exit for
end if
next
if existpk = false then
for each key in table.keys
if key.Name = sIndexName then
existpk = true
exit for
end if
next
end if
if existpk = false then
'Name Key field
pk_name = sIndexName
pk_code = sIndexCode
pk_constraint_name = "PK_" + TABLE.Code
'Create PK
set pk_obj = table.Keys.CreateNew()
pk_obj.Name = pk_name
pk_obj.Code = pk_code
pk_obj.ConstraintName = pk_constraint_name
pk_obj.Primary = true
'add columns
index = 0
while index < index_col_num
for each col in table.Columns
if col.Name = index_cols_name(index) then
pk_obj.Columns.Add col
end if
Next
index = index + 1
Wend
end if
end if
next
NEXT