The procedure [cont].[upsert_bulk_meta_fields]
is a SQL Server stored procedure designed for bulk upsert operations on metadata fields from source tables into a target metadata table. The procedure accepts various parameters to define the source and target tables, as well as the metadata structure, and supports batch processing.
Key Steps in the Procedure:
- Initialization: Sets up various parameters and retrieves the source table type.
- Source Table Identification: Uses the
sourceSchema
andsourceName
to determine the table type fromINFORMATION_SCHEMA.TABLES
. - Default Values and Setup:
- Sets default values for parameters if they are
NULL
. - Prepares SQL statements and variables for processing.
- Sets default values for parameters if they are
- Dynamic SQL for Table Creation:
- Generates dynamic SQL to create a target table if it doesn’t exist.
- Constructs the SQL for processing entity and metadata fields dynamically based on the inputs.
- Field Processing Loop:
- Loops through the columns in the source table or a specified list of measures.
- For each field, it creates a temporary table with metadata information (
[temp].[upsert_bulk_meta]
).
- Batch Processing:
- Processes rows in batches (default size 1000) to manage large data volumes efficiently.
- For each batch, metadata is calculated and upserted into the target table using another stored procedure
cdm.upsert_meta
.
- Error Handling:
- The procedure includes comprehensive error handling using
TRY...CATCH
blocks, raising detailed error messages if something goes wrong.
- The procedure includes comprehensive error handling using
Purpose:
The procedure is primarily used to insert or update metadata fields in bulk from a source table to a target table, handling large datasets efficiently through batching. It ensures that the target table is created if it doesn’t exist, processes fields dynamically, and handles potential errors gracefully.