Function CheckConstraintNameIsUnique(Parent, ChildType, ForChild, Name, Document) 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) Then CheckConstraintNameIsUnique = False Document.Write("Non Unique for " & Table.Property("Name").AsString & " NewName = " & Name) Exit Function End If Next If Not NameIsUnique(Table, ChildType, ForChild, Name, Document) Then CheckConstraintNameIsUnique = False Document.Write("Non Unique for " & Table.Property("Name").AsString & " NewName = " & Name) Exit Function End If CheckConstraintNameIsUnique = True End Function Function NameIsUnique(Parent, ChildType, ForChild, Name, Document) 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.Property("Name").AsString = Name Then NameIsUnique = False Exit Function End If End If Next NameIsUnique = True End Function Sub RenameChildren(Parent, ChildType, Prefix, ParentName, Framework, ByRef Num, Document) 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 OldName = Child.Property("Name").AsString If ChildType = "Key Constraint" Then If Child.Property("Type").AsString = "Primary" Then Prefix = "PK" Else Prefix = "UQ" End If End If NewName = Prefix If Prefix <> "PK" Then Num = Num + 1 If Len(Num) = 1 Then NewName = NewName & "0" End If NewName = NewName & Num 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" Then UniqueName = CheckConstraintNameIsUnique(Parent, ChildType, Child, NewName, Document) Else UniqueName = NameIsUnique(Parent, ChildType, Child, NewName, Document) End If Loop While Not UniqueName If NewName <> OldName Then Document.Write(vbTab & "Changing " & ChildType & " Name " & ParentName & "." & OldName & " to " & NewName & vbLf) Set Prop = Child.Property("Name") Prop.FromString(NewName) Call Child.SetProperty("Name", Prop) 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 Set Columns = Table.Children("Column") For Each Column in Columns Num = 0 Call RenameChildren(Column, "Lob Storage", "LOB", TableName & "." & Column.Property("Name").AsString, Framework, Num, Document) Next ' Run check constraints back to back and keep num incrementing Num = 0 For Each Column in Columns Call RenameChildren(Column, "Check Constraint", "CK", TableName, Framework, Num, Document) Next Call RenameChildren(Table, "Check Constraint", "CK", TableName, Framework, Num, Document) Num = 0 Call RenameChildren(Table, "Index", "IX", TableName, Framework, Num, Document) Num = 0 Call RenameChildren(Table, "Key Constraint", PrePrefix, TableName, Framework, Num, Document) Num = 0 Call RenameChildren(Table, "Relation", "FK", TableName, Framework, Num, Document) 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