如何在DDL脚本中使用TaggedValue及在模板中访问表的TaggedValue?
Hey there! Let's tackle your questions since you're already hands-on with modifying DDL templates—this should align perfectly with your current workflow.
1. General Approach to Using TaggedValues in DDL Scripts
TaggedValues are your go-to for attaching custom metadata to model elements (like tables, columns, or constraints) that isn't part of standard ER/UML models. To leverage them in DDL, the process boils down to two core steps:
- Add the TaggedValue to your model elements: For example, create a tag like
IsTemporalTablefor tables, setting values likeTrueorFalseto flag whether temporal support is needed. - Modify your DDL template to access the tag: Use conditional logic in the template to generate different SQL code based on the TaggedValue's value.
2. Accessing Table TaggedValues in Your DDL Template for Temporal Tables
Since you're already tweaking the default DDL template, I'll assume you're using Enterprise Architect (the most common tool for this kind of customization)—here's how to implement the temporal table logic:
Step 1: Add the TaggedValue to Your Tables
In EA:
- Right-click the target table > Properties > Navigate to the Tagged Values tab.
- Click Add to create a new tag, name it
IsTemporalTable, and set its value toTruefor tables needing temporal support, orFalse(leave blank if you want a non-temporal default).
Step 2: Modify the DDL Template
Open the template editor via Settings > Code Generation > Templates, then locate the section where tables are created (usually labeled something like Table Creation). Here's a modified example using SQL Server's temporal table syntax:
CREATE TABLE %TableName% ( %ListColumns% -- Add temporal system columns only if the tag is enabled %if TaggedValue("IsTemporalTable") == "True" % SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL, SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL, PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime) %endIf% ) -- Enable system versioning for tagged tables %if TaggedValue("IsTemporalTable") == "True" % WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.%TableName%_History)) %endIf%;
Key Tips for Success:
- Match Tag Names Exactly: While EA is case-insensitive for tag names, sticking to consistent spelling (e.g.,
IsTemporalTablenotistemporaltable) avoids unexpected issues. - Adjust for Your Database: Temporal table syntax varies:
- MySQL uses
WITH SYSTEM VERSIONINGdirectly in theCREATE TABLEblock. - Oracle uses Flashback Data Archive, requiring
ALTER TABLE %TableName% ARCHIVEafter creation.
Tweak the template code to fit your target database's requirements.
- MySQL uses
- Fallback for Missing Tags: If you want tables without the
IsTemporalTabletag to default to non-temporal, adjust the condition to check for tag existence first:%if TaggedValueExists("IsTemporalTable") AND TaggedValue("IsTemporalTable") == "True" %
Test the Template
Generate DDL for two tables—one with IsTemporalTable=True and one without—to confirm that temporal table code is only added where intended.
内容的提问来源于stack exchange,提问作者Ruudjah

