Purpose
The get.myArchive stored procedure is designed to retrieve archived data from a specified DataMart view, allowing for historical analysis and structured retrieval. The procedure integrates data retrieval logic, version control, and debugging capabilities to ensure flexible and efficient access to archived data.
Key Features
- Parameterised Input: Supports multiple parameters for flexible data extraction, including:
@dataMartand@viewNameto define the target dataset.@myDateto specify the date for archival retrieval.@versionfor accessing different stored versions of the data.@whereClauseto filter results dynamically.@debugmode for enhanced logging and issue tracking.@tableSchemaand@viewSchemafor specifying schema constraints.
- Dynamic View Construction:
- Constructs the appropriate view name based on the provided parameters.
- Uses a hashed string (
MD5) for unique identifiers where required.
- Archive Management:
- Identifies and retrieves archived views dynamically.
- Calls
put.myarchiveto manage data persistence. - Verifies view existence before retrieval.
- Error Handling & Debugging:
- Implements structured error handling with SQL Server’s
TRY...CATCHmechanism. - Debug mode enables SQL statements and process flow visibility.
- Implements structured error handling with SQL Server’s
- Secure Execution:
- Uses
SUSER_SNAME()to track execution user. - Enforces
NOCOUNT ONto reduce interference with SELECT statements.
- Uses
Workflow
- Parameter Validation & Default Assignments:
- Ensures parameters have appropriate values or assigns defaults.
- Constructs
@myDataViewbased on the provided@dataMartand@viewName.
- Data Archival Handling:
- Calls
put.myarchiveto ensure the requested archive exists. - Constructs the target archive view name using the date (
@archiveDate).
- Calls
- View Existence Check:
- Uses
OBJECT_IDto validate the existence of the requested archive.
- Uses
- Data Retrieval:
- If the view exists, executes
get.myviewto fetch the archived dataset.
- If the view exists, executes
- Error Handling:
- Captures and raises errors with relevant details, including stored procedure name and table reference.
Use Case
This procedure is ideal for historical data retrieval where users need to extract archived snapshots of DataMart views dynamically. It provides a structured approach to accessing versioned data and ensures robust error handling in case of missing or malformed requests.
Sample Execution of get.myarchive Stored Procedure
Before executing the stored procedure, ensure that your connection is established to the OnDemand data source:
🔹 Server: bmt-dwh-uks-syn-prd-ondemand.sql.azuresynapse.net
🔹 Database: dwh_silver
Example Execution: Retrieving an Archived Project Details View
EXEC [get].[myarchive]
@token = 'abc123',
@dataMart = 'project',
@viewName = 'details',
@myDate = '2025-01-13',
Explanation of Parameters
| Parameter | Value | Description |
|---|---|---|
@token | 'abc123' | Report_ID authentication token (example value). |
@dataMart | 'project' | Specifies the DataMart to query. |
@viewName | 'details' | Specifies the view within the DataMart. |
@myDate | '2025-01-13' | Retrieves data archived on this date. |
Expected Outcome
- The procedure verifies the existence of an archived view:
svw.project_details_20250113
- If found, it calls
get.myviewto retrieve the data. - Debug mode prints useful messages to track execution flow.
- If an error occurs (e.g., view not found), it raises an error with detailed information.
This execution retrieves project details as of 13th January 2025.