Function CheckNameIsUniqueAcrossColumns(Parent, ChildType, ForChild, Name, Document, PropName) Dim Table Set Table = Parent If Parent.TypeName = "Column" Then Set Table = Parent.Owner End If Dim Columns Dim Column Set Columns = Table.Children("Column") For Each Column in Columns If Not NameIsUnique(Column, ChildType, ForChild, Name, Document, PropName) Then CheckNameIsUniqueAcrossColumns = False Exit Function End If Next If ChildType = "Check Constraint" Then If Not NameIsUnique(Table, ChildType, ForChild, Name, Document, PropName) Then CheckNameIsUniqueAcrossColumns = False Exit Function End If End If CheckNameIsUniqueAcrossColumns = True End Function Function NameIsUnique(Parent, ChildType, ForChild, Name, Document, PropName) Dim Children Dim Child Set Children = Parent.Children(ChildType) For Each Child in Children Continue = True If ChildType = "Index" And Child.HasProperty("Key") Then Continue = False End If If Continue Then If Not Child.Equals(ForChild) And Child.HasProperty(PropName) Then If Child.Property(PropName).AsString = Name Then NameIsUnique = False Exit Function End If End If End If Next NameIsUnique = True End Function Sub RenameChildren(Parent, ChildType, Prefix, ParentName, Framework, ByRef Num, Document, PropName, ByRef AltNum) Dim PropertyValue Set p = Framework.CreatePropertyValue(ChildType, "Generate Name") p.FromBoolean(True) Dim Children Dim Child Set Children = Parent.Children(ChildType) For Each Child in Children Continue = True If ChildType = "Index" And Child.HasProperty("Key") Then Continue = False End If If Continue Then Do UseAltCounter = false UsePrefix = Prefix OldName = "" If Child.HasProperty(PropName) Then OldName = Child.Property(PropName).AsString End If If ChildType = "Key Constraint" Then If Child.Property("Type").AsString = "Primary" Then UsePrefix = "PK" Else UsePrefix = "UQ" End If End If If ChildType = "Check Constraint" Then If Child.Property("Type").AsString = "NotNull" Then UsePrefix = "NN" UseAltCounter = true End If End If NewName = UsePrefix If UsePrefix <> "PK" Then If UseAltCounter Then AltNum = AltNum + 1 NumToUse = AltNum Else Num = Num + 1 NumToUse = Num End If If Len(NumToUse) = 1 Then NewName = NewName & "0" End If NewName = NewName & NumToUse End If NewName = NewName & "_" & ParentName NewName = Replace(NewName, ".", "_") NewName = UCase(Left(NewName, 30)) If Len(NewName) = 30 And Right(NewName, 1) = "_" Then NewName = Left(NewName, 29) End If If ChildType = "Check Constraint" Or ChildType = "Lob Storage" Then UniqueName = CheckNameIsUniqueAcrossColumns(Parent, ChildType, Child, NewName, Document, PropName) Else UniqueName = NameIsUnique(Parent, ChildType, Child, NewName, Document, PropName) End If Loop While Not UniqueName If NewName <> OldName Then Document.Write(vbTab & "Changing " & ChildType & " " & PropName & " " & ParentName & "." & OldName & " to " & NewName & vbLf) Set pName = Framework.CreatePropertyValue(ChildType, PropName) pName.FromString(NewName) Call Child.SetProperty(PropName, pName) End If Call Child.SetProperty("Generate Name", p) End If Next End Sub Sub RenameChildTypes(Table, Document, Framework) TableName = Table.Property("Name").AsString ' rename column LOB storage clauses Dim Columns Dim Column Dim Clause Dim Num Dim AltNum Set Columns = Table.Children("Column") Num = 0 AltNum = 0 For Each Column in Columns Call RenameChildren(Column, "Lob Storage", "LOB", TableName, Framework, Num, Document, "Name", AltNum) Next Num = 0 AltNum = 0 For Each Column in Columns Call RenameChildren(Column, "Lob Storage", "LX", TableName, Framework, Num, Document, "Index Name", AltNum) Next ' Run check constraints back to back and keep num incrementing Num = 0 AltNum = 0 For Each Column in Columns Call RenameChildren(Column, "Check Constraint", "CK", TableName, Framework, Num, Document, "Name", AltNum) Next Call RenameChildren(Table, "Check Constraint", "CK", TableName, Framework, Num, Document, "Name", AltNum) Num = 0 Call RenameChildren(Table, "Index", "IX", TableName, Framework, Num, Document, "Name", AltNum) Num = 0 Call RenameChildren(Table, "Key Constraint", PrePrefix, TableName, Framework, Num, Document, "Name", AltNum) Num = 0 Call RenameChildren(Table, "Relation", "FK", TableName, Framework, Num, Document, "Name", AltNum) End Sub Sub Evaluate_OnLoad() Set Context = CreateObject("SCF.ScriptContext") Set Document = Context.ScriptDocument Set ThisScript = Context.Object Set Options = Context.Options Set Model = ThisScript.Model Set Framework = CreateObject("SCF.ScriptFramework") Document.Write("Updating Model " & Model.AsObject.Property("Name").AsString & " Constraint Names..." & vbLf) Model.BeginTransaction("Change Constraint Names") Set Tables = Model.AsObject.Children("Table") For Each Table In Tables Call RenameChildTypes(Table, Document, Framework) Next Model.EndTransaction() Document.Write("Updating Model " & Model.AsObject.Property("Name").AsString & " Done!" & vbLf) End Sub