You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DDL脚本中使用TaggedValue及在模板中访问表的TaggedValue?

Using TaggedValues in DDL Scripts & Generating Temporal Tables Dynamically

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 IsTemporalTable for tables, setting values like True or False to 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 to True for tables needing temporal support, or False (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., IsTemporalTable not istemporaltable) avoids unexpected issues.
  • Adjust for Your Database: Temporal table syntax varies:
    • MySQL uses WITH SYSTEM VERSIONING directly in the CREATE TABLE block.
    • Oracle uses Flashback Data Archive, requiring ALTER TABLE %TableName% ARCHIVE after creation.
      Tweak the template code to fit your target database's requirements.
  • Fallback for Missing Tags: If you want tables without the IsTemporalTable tag 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:57:22