Code that automatically sets the IDENTITY property (PowerDesigner script)

Source: Internet
Author: User
Script ' *****************************************************************************
' File: Setidentity.vbs
' Version: 1.0
' Copyright: Floodzhu (floodzhu@hotmail.com), 2004.12.31
' function: Iterate through all the tables in the physical model, setting the fields that are primary keys but not foreign keys to identity, and apply to
' Physical model is the type of MS SQL Server.
' Usage: Open the physical model, run this script (ctrl+shift+x)
' Note: I have two habits, one is to define the primary key of all tables as the type of int that grows, and the other is to define
' A domain name ID that sets the domain of all PrimaryKey fields to IDs when designing the conceptual model
Type
'
' If I make the above setting, I need to manually set the identity when I convert to the physical model.
' The dumbest way to do this is to set up a table with a table, and the easiest way to do this is to have the physical model directly
' To set it up. A small disadvantage of setting up domain is that if the field is not a primary key, it is not a raw
' As a foreign key, but a generic field, such as a PID that represents a tree structure, it is also set to
' Identity, however, because there are fewer such fields, and an error occurs when the database is generated to alert
' You make corrections, so you can easily pass without hiding mistakes, so it's a good way.
'
' Use the following code to give you a third choice without any errors.
'*****************************************************************************
Dim model ' current model '
Set model = Activemodel

If (model is nothing) Then
MsgBox "There is no current Model"
ElseIf not model. IsKindOf (Pdpdm.cls_model) Then
MsgBox "The" "" "" The "" "is not" physical Data model. "
Else
Processtables model
End If

'*****************************************************************************
' Function: Processtables
' Function: Recursively iterate through all the tables
'*****************************************************************************
Sub Processtables (folder)
' Process the tables in the model
Dim table
For each table in Folder.tables
If not table. Isshortcut Then
Processtable table
End If
Next

' Recursively for subdirectories
Dim subfolder
For each subfolder in folder. Packages
Processtables subfolder
Next
End Sub

'*****************************************************************************
' Function: processtable
' Function: Traverse all fields of the specified table, if the field is a primary key but not a foreign key, set to identity
'*****************************************************************************
Sub Processtable (table)
Dim col
For each col in table. Columns
' Set to identity for fields that are primary keys and not foreign keys (self-growing type)
If Col. Primary and not Col. ForeignKey Then
Col. Identity = True
End If
Next
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.