Summary
The [cdm].[post_insert]
stored procedure is responsible for inserting a new metadata row into a specified table using a structured JSON payload. It dynamically builds the insert logic using the column metadata from cont.table_templates
, including default values and computed checksum logic.
This procedure is a core component of the CDM metadata engine, providing consistency, reusability, and integrity for metadata ingestion.
π§ Key Steps
- Initialise Context & Defaults
- Sets default values for
@authoredby
,@sourceid
, and@create_date
. - Records the name of the procedure being executed and the current timestamp.
- Sets default values for
- Parse Target Table
- Splits
@tableName
into schema and object name. - Constructs a fully qualified name in
[schema].[table]
format.
- Splits
- Generate CHECKSUM
- Dynamically builds a hash expression using the fields marked with
is_checksum = 'YES'
in the template. - Computes an MD5 checksum to support later change detection.
- Dynamically builds a hash expression using the fields marked with
- Prepare Columns and Values
- Constructs column and value lists by:
- Pulling template metadata
- Resolving values from the JSON payload
- Applying default values for fields like
EXPIRY_DATE
,ACTIVE_FLAG
, andSOURCE_SYSTEM
- Constructs column and value lists by:
- Execute INSERT
- Builds and runs a dynamic SQL statement to insert the new row.
π§ͺ Example Usage
This procedure is called internally by:
[cdm].[patch_upsert]
[cdm].[upsert]
Direct use is rare, but it can be manually tested:
EXEC [cdm].[post_insert]
@tableName = 'cdm.project_codes',
@payload = N'{
"OBJECT_SEQ": "project_123-456",
"META_TYPE": "codes",
"ATTRIBUTE": "category",
"VALUE": "internal"
}',
@authoredby = 'jkellett',
@debug = 1;
π¦ Parameters
Parameter | Description |
---|---|
@tableName | Fully qualified or short name of the target table |
@payload | JSON object containing metadata to insert |
@tableType | Template category used to build column/value list (e.g. meta , item ) |
@debug | If 1 , prints debug messages to the console |
@authoredby | Name of the author stored in the row metadata |
@sourceid | External system source ID for lineage tracking |
@create_date | Optional creation timestamp, default is system datetime |
π§© Template-Driven Logic
This procedure relies on the cont.table_templates
structure, using it to:
- Match column names
- Extract corresponding JSON parameter mappings
- Determine field defaults and checksum contribution
β Features
- π Ensures metadata integrity with MD5-based CHECKSUM generation
- π¦ Designed for JSON payloads β schema is dynamically inferred
- π Supports flexible domain variations through
@tableType
- π§Ύ Automatically assigns:
EXPIRY_DATE = '2099-12-31'
ACTIVE_FLAG = 1
SOURCE_SYSTEM
,CREATE_DATE
,CHECKSUM
values