ER/スタジオロゴ
ER/スタジオロゴ
ホーム > リソースセンター > 参照値からルックアップテーブルを生成

参照値からルックアップテーブルを生成

と ER/Studio データアーキテクト, we can use the Data Dictionaries to define Reference Values and to bind them to our Attributes|Columns.

Reference Values
Reference Values

If we generate a JSONインスタンス, we can directly use these Reference Values in our JSONスキーマ:

JSON Tab Options
JSON Tab Options
JSONスキーマ
JSONスキーマ

Moreover, we can used it as documentation with the Tables, or we can generate objects and SQL from these Reference Values.

Report
Report

For instance, I’ll share a script to generate the lookup tables for all the columns of the selected Tables which use Reference Values Lists. With 物理データモデル, it also generates in the Post SQL tab of the created Reference table, the Insert statements.

Usage

  • Select the Entities|Tables which contain Attributes|Columns which use Reference Values
  • Run the macro
    • It generates one entity|table per attribute|column using a Reference Values List
    • It generates the 2 attributes|columns for the lookup entity|table
Reference tables
Reference tables

It generates the INSERT statements for each list item for a Physical Data Model

INSERT statements
INSERT statements

As usual, feel free to update the code to get your custom requirements.

スクリプト

'#Language "WWB-COM"
'
'MACRO TITLE: Generate Lookup Table From Reference Values
' 
'This macro generates a lookup Table|Entity for the
'	columns|attributes which use a list of Reference Values,
'	for the selected table|entities.
'With Physical models, it also generates the Insert statements
'	for the values in the 'Post SQL' Table Tab.
'
' Known limitations:
' The SQL code for the Insert should be updated depending on the DB
'	platform used
'
'-------------------------------------------------------------------

オプション 明示的

コンスト REF_OBJECT_PREFIX$ = "REF_"
コンスト GENERATE_INSERT として ブーリアン = True

Sub メイン
	薄暗い MyDiagram として ダイアグラム
	薄暗い MyModel として モデル
	薄暗い MyEntity として エンティティ
	薄暗い MyEntityDisplay として EntityDisplay
	薄暗い MySubModel として SubModel
	薄暗い MySelObject として SelectedObject
	薄暗い MyAttribute として AttributeObj
	薄暗い MyReferenceValue として ReferenceValue

	薄暗い MyRefEntity として エンティティ
	薄暗い MyRefID として AttributeObj
	薄暗い MyRefDesc として AttributeObj
	薄暗い MyRelationship として 人間関係
	薄暗い MyDomain として Domain
	薄暗い MyDictionary として Dictionary

	薄暗い ID%, iExtract%
	薄暗い 論理的 として ブーリアン
	薄暗い ObjectName$, SubObjectName$
	薄暗い ObjType%, RefValueID%
	薄暗い vp として ValuePair
	薄暗い sPostSQL$
	薄暗い bGenerateInsert として ブーリアン

	デバッグ。.クリア

	'Get the current diagram.
	セット MyDiagram = DiagramManager.ActiveDiagram
	
	'Get the current model.
	セット MyModel = MyDiagram.ActiveModel
	
	'Get the current submodel.
	セット MySubModel = MyModel.ActiveSubModel
	
	'Determine if the model is logical or physical.
	Logical = MyModel.Logical

	bGenerateInsert = GENERATE_INSERT そして Not 論理的
	
	'Iterate through all the selected entities in the submodel
	について Each MySelObject In MySubModel.SelectedObjects
		
		'Get type of the selected object.
		ObjType = MySelObject.Type
		
		'We only want to get the entities, so check the object type.
		もし ObjType = 1 では
		
			'Get the ID of the selected object.
			ID = MySelObject.ID
	
			'Now, get the actual entity object with this ID.
			セット MyEntity = MyModel.Entities.Item(ID)

			'If the model is logical, get the entity name. 
			'If it is physical, get the table name.
			ObjectName = IIf(Logical, MyEntity.EntityName, MyEntity.TableName)

			'Now, get the actual entity display object with the name of the Entity.
			セット MyEntityDisplay = MySubModel.EntityDisplays.Item(ObjectName)

			Debug.プリント ObjectName

			' Loop the Attributes
			について Each MyAttribute In MyEntity.Attributes
				
				'If the model is logical, get the attribute name. 
				'If it is physical, get the column name.
				SubObjectName = IIf(Logical, MyAttribute.AttributeName, MyAttribute.ColumnName)
				Debug.プリント vbTab & SubObjectName & ": " & MyAttribute.ReferenceValueId & " / " & MyAttribute.DomainId

				' Only check the attributes which are not FK yet
				もし Not MyAttribute.ForeignKey では
					
					' Check if a domain is bound if no ref value set
					もし MyAttribute.ReferenceValueId = 0 そして MyAttribute.DomainId > 0 では
	
						' Try to get a reference to the Domain from the LOCAL DD
						セット MyDomain = MyDiagram.Dictionary.Domains.Item(MyAttribute.DomainId)

						' Check if domain exists
						もし Not MyDomain そうなのか? 何もない では
							
							デバッグ。.プリント vbTab & vbTab & "Local dictionary: " & MyDomain.ReferenceValueId
							' Copy the ReferenceValueId from the Domain to the Attribute|Column
							MyAttribute.ReferenceValueId = MyDomain.ReferenceValueId

						その他
	
							' Loop the Enterprise DD
							について Each MyDictionary In MyDiagram.EnterpriseDataDictionaries
								
								' Try to get a reference to the Domain from an Enterprise DD
								セット MyDomain = MyDictionary.Domains.Item(MyAttribute.DomainId)
		
								' Check if domain exists
								もし Not MyDomain そうなのか? 何もない では

									デバッグ。.プリント vbTab & vbTab & "Enterprise dictionary [" & MyDictionary.Name & "]: " & MyDomain.ReferenceValueId
									' Copy the ReferenceValueId from the Domain to the Attribute|Column
									MyAttribute.ReferenceValueId = MyDomain.ReferenceValueId

									' Break the loop
									出口 について

								終了 もし

							次のページ MyDictionary
	
						終了 もし
	
					終了 もし

					' Check if a Reference Value is bound to the Attribute
					もし MyAttribute.ReferenceValueId > 0 では
	
						sPostSQL = ""
	
						' Get the ReferenceValue
						セット MyReferenceValue = MyAttribute.GetReferenceValue
						RefValueID = MyAttribute.ReferenceValueId
	
						もし Not MyReferenceValue そうなのか? 何もない では
							
							' Check if the ReferenceValueId is linked to a list
							もし Not MyReferenceValue.IsRange では
							
								' Check if a Lookup object already exists
								セット MyRefEntity = MyModel.Entities(REF_OBJECT_PREFIX + SubObjectName)
			
								もし MyRefEntity そうなのか? 何もない では
			
									' Create the Lookup object
									セット MyRefEntity = MyModel.Entities.AddEx(MyEntityDisplay.HorizontalPosition + 50, MyEntityDisplay.VerticalPosition)
									MyRefEntity.EntityName = REF_OBJECT_PREFIX + SubObjectName
									MyRefEntity.TableName = MyRefEntity.EntityName
	
									MyRefEntity.Definition = "Lookup Table for column '" & SubObjectName & "' of the Table '" & ObjectName & "'"
									MyRefEntity.Note = IIf(bGenerateInsert, "Values", "Extract") & " of the Reference Value '" & MyReferenceValue.Name & "': " & vbCrLf
									iExtract = 0
									について Each vp In MyReferenceValue.Values
										
										Debug.プリント Chr(9) & Chr(9) & vp.Value
										Debug.プリント Chr(9) & Chr(9) & vp.ValueDescription
										MyRefEntity.Note = MyRefEntity.Note & vbCrLf & vp.Value & ": " & vp.ValueDescription
	
										' Generate Insert
										もし bGenerateInsert では
											sPostSQL = sPostSQL & "INSERT INTO " & MyRefEntity.TableName & "( " & SubObjectName & ", Description ) VALUES ( """ & vp.Value & """, """ & vp.ValueDescription & """ )" & vbCrLf
										終了 もし
	
										' We only take the 5 firsts
										iExtract = iExtract + 1
										もし Not bGenerateInsert そして (iExtract = 5) では
											
											MyRefEntity.Note = MyRefEntity.Note & vbCrLf & "..."
											出口 について
	
										終了 もし
	
									次のページ vp
	
									' Add INSERT into the PostSQL of the lookup table
									もし bGenerateInsert では
	
										MyRefEntity.PostSQL = sPostSQL
										MyRefEntity.GenPostSQL = False
	
									終了 もし
	
									' Add the Reference value column
									セット MyRefID = MyRefEntity.Attributes.Add(SubObjectName, 真)
									MyRefID.Datatype = MyAttribute.Datatype
									MyRefID.DataLength = MyAttribute.DataLength
									MyRefID.DataScale =  MyAttribute.DataScale
									' add the Description column
									セット MyRefDesc = MyRefEntity.Attributes.Add("Description", False)
									MyRefDesc.Datatype = "VARCHAR"
									MyRefDesc.DataLength = 1024
									MyRefDesc.NullOption = "NULL"
	
									' Add the relationship
									セット MyRelationship = MyModel.Relationships.Add(MyRefEntity.EntityName, ObjectName, IIf(MyAttribute.NullOption = "NOT NULL", 1, 3))
									MyAttribute.ReferenceValueId = RefValueID
	
								その他
									
									デバッグ。.プリント REF_OBJECT_PREFIX + SubObjectName & " already exists!"

									' Get the Reference value column
									セット MyRefID = MyRefEntity.Attributes.Item(SubObjectName)

									' Check if the Attribute|Column exists
									もし Not MyRefID そうなのか? 何もない では

										' Add the relationship
										セット MyRelationship = MyModel.Relationships.Add(MyRefEntity.EntityName, ObjectName, IIf(MyAttribute.NullOption = "NOT NULL", 1, 3))
										MyAttribute.ReferenceValueId = RefValueID

										' Update the Definition of the lookup object
										MyRefEntity.Definition = "Lookup Table for column '" & SubObjectName & "' of the Table '" & ObjectName & "'" & vbCrLf & vbCrLf & MyRefEntity.Definition

									終了 もし

								終了 もし
	
							終了 もし
	
						終了 もし
	
					終了 もし

				終了 もし

			次のページ MyAttribute

		終了 もし
		
	次のページ MySelObject

	Debug.プリント ""
	Debug.プリント "End!"
	MsgBox "Done.", vbInformation

終了 サブ

ボーナス

A ショートビデオ ER/Studio Data Architectでスクリプトからマクロを作成する方法を説明します:

著作権 © 2026 Idera, Inc.
Hi! Need help finding something?
ER/Studio
Here to help you find your way