Summary
The [cdm].[create_table]
stored procedure dynamically builds SQL tables using metadata templates from cont.table_templates
. It is used to create consistent core, meta
or item
tables within a specified schema, complete with partitioning, indexes, and insertion triggers for data integrity and metadata logging.
This approach ensures repeatability, consistency, and automated setup of foundational table structures across domains.
π§ Key Steps
- Set Context & Defaults
- Ensures defaults for optional parameters (
@tableSchema
,@tableType
, etc.) - Establishes current session metadata such as procedure name and author.
- Ensures defaults for optional parameters (
- Create Schema (if missing)
- Validates and creates the target schema dynamically.
- Define Table Name
- Constructs a slugged, logical table name using
entityName
,domainSuffix
, andtableType
.
- Constructs a slugged, logical table name using
- Build Table from Template
- Reads the
cont.table_templates
definition to dynamically assemble:- Column list
- Default values
- Primary key structure (with
EXPIRY_DATE
if partitioning is enabled)
- Reads the
- Create Table
- Executes a
CREATE TABLE
statement using the constructed SQL.
- Executes a
- Add Indexes
- Conditionally creates:
- Indexed columns defined in the template
CHECKSUM
index for lookupEXPIRY_DATE
index for partitioning
- Conditionally creates:
- Generate Trigger
- Adds an
INSTEAD OF INSERT
trigger to:- Populate
id
withNEWID()
if missing - Auto-fill default fields (
CREATE_DATE
,SOURCE_SYSTEM
,CHECKSUM
, etc.)
- Populate
- Adds an
- Log to Metadata
- Inserts an entry into
[log].[list_table]
for governance and traceability viaupsert.table_list
.
- Inserts an entry into
- Error Handling
- Catches and raises detailed errors including procedure name and user context.
π§ͺ Example Usage
EXEC [cdm].[create_table]
@entityName = 'employee',
@domainSuffix = 'codes',
@tableType = 'meta',
@debug = 1;
π§© Template Notes
- Templates from
cont.table_templates
must define:column_name
,data_type
,default_value
,is_nullable
- Flags for
is_idx
andis_checksum
to drive index and hash logic
π Parameters Overview
Parameter | Description |
---|---|
@entityName | Logical prefix for table naming |
@domainSuffix | Optional suffix (e.g. ‘absence’, ‘contract’) |
@debug | Set to 1 for verbose outputs |
@tableSchema | Schema under which the table will be created |
@tableType | Table pattern, e.g. 'meta' or 'item' |
@valuesize | String to replace {valuesize} placeholder in templates |
@authoredby | Creator identity for logging |
@sourceid | Source system ID used in metadata |
@partition_me | If 1, enables partitioning by [EXPIRY_DATE] |
π Output
- A new table in the specified schema and name
- Accompanying indexes and trigger
- Entry in the list_table log for traceability