Repository: CDCgov/NEDSS-DataReporting
Pull request #376: CNDE-2496 LDF_TETANUS_DATAMART
Author: login: agonzalez-cloud39
Created: 2025-04-23T17:23:12Z | Merged: 2025-04-28T14:43:27Z

## Notes

Table and stored procedure to fulfill LDF_TETANUS table. 
Also includes a fix in stored procedure: sp_ldf_foodborne_datamart_postprocessing (wrong reference to a global temp table)

## JIRA

- **Related story**: [Jira Ticket](https://cdc-nbs.atlassian.net/browse/CNDE-2496)

## Checklist

- [x] PR focuses on a single story.
- [x] New unit tests added and ensured they pass.
- [ ] Service has been tested in local and it works as expected.
- [ ] Documentation has been updated for this code change (if needed).
- [ ] Code follows the Java Coding Conventions (https://www.oracle.com/java/technologies/javase/codeconventions-programmingpractices.html).

## Types of changes

What types of changes does this PR introduces?

- [ ] Bugfix
- [x] New feature
- [ ] Breaking change

## Testing

- [x] Does this PR has >90% code coverage?
- [ ] Is the screenshot attached for code coverage?
- [ ] Does the `gradle build` pass in your local? 
- [ ] Is the `gradle build` logs attached?


Diff:
diff --git a/db/upgrade/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing.sql b/db/upgrade/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing.sql
index 1e5ce483a..341ef5b58 100644
--- a/db/upgrade/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing.sql
+++ b/db/upgrade/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing.sql
@@ -185,7 +185,7 @@ BEGIN
                 ON b.INVESTIGATION_UID = i.CASE_UID 
             INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
                 ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
-            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
+            INNER JOIN [dbo].V_CONDITION_DIM c WITH (NOLOCK)
                 ON c.CONDITION_KEY = g.CONDITION_KEY
             INNER JOIN [dbo].D_PATIENT p WITH (NOLOCK)
                 ON p.PATIENT_KEY = g.PATIENT_KEY;
@@ -354,7 +354,7 @@ BEGIN
             SET
                 @PROC_STEP_NO = @PROC_STEP_NO + 1;
             SET
-                @PROC_STEP_NAME = 'GENERATING ##FOODBORNE_TA';
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_foodborne_ta;
             
             EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_foodborne_ta +''', ''U'')  IS NOT NULL
             BEGIN
@@ -449,7 +449,7 @@ BEGIN
             SET
                 @PROC_STEP_NO = @PROC_STEP_NO + 1;
             SET
-                @PROC_STEP_NAME = 'GENERATING ##FOODBORNE_SHORT_COL';
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_foodborne_short_col;
             
             EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_foodborne_short_col +''', ''U'')  IS NOT NULL
             BEGIN
@@ -507,7 +507,7 @@ BEGIN
                             PATIENT_LOCAL_ID,
                             DISEASE_NAME,
                             DISEASE_CD
-                        INTO ' + @global_temp_foodborne_ta +'
+                        INTO ' + @global_temp_foodborne_short_col +'
                         FROM #ALL_FOODBORNE_SHORT_COL';
                     END
                 END
@@ -521,7 +521,7 @@ BEGIN
                         PATIENT_LOCAL_ID,
                         DISEASE_NAME,
                         DISEASE_CD                 
-                    INTO ' + @global_temp_foodborne_ta +'
+                    INTO ' + @global_temp_foodborne_short_col +'
                     FROM #ALL_FOODBORNE_SHORT_COL';
                 END;
 
@@ -598,7 +598,7 @@ BEGIN
             SET
                 @PROC_STEP_NO = @PROC_STEP_NO + 1;
             SET
-                @PROC_STEP_NAME = 'GENERATING ##FOODBORNE';
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_foodborne;
 
             EXECUTE  [dbo].[sp_MERGE_TABLES] 
                 @INPUT_TABLE1= @global_temp_foodborne_short_col
diff --git a/db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql b/db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
new file mode 100644
index 000000000..cf9e9639b
--- /dev/null
+++ b/db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
@@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].V_CONDITION_DIM c WITH (NOLOCK)
+                ON c.CONDITION_KEY = g.CONDITION_KEY
+            INNER JOIN [dbo].D_PATIENT p WITH (NOLOCK)
+                ON p.PATIENT_KEY = g.PATIENT_KEY;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #LINKED_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+            
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS';
+            
+            IF OBJECT_ID('#ALL_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS;
+
+            SELECT 
+                a.COL1,
+                a.INVESTIGATION_UID,          
+                a.CODE_SHORT_DESC_TXT,     
+                a.LDF_UID,                       
+                a.CODE_SET_NM,             
+                a.LABEL_TXT,               
+                a.DATA_SOURCE,              
+                a.CDC_NATIONAL_ID,          
+                a.BUSINESS_OBJECT_NM,       
+                a.CONDITION_CD,              
+                a.CUSTOM_SUBFORM_METADATA_UID,     
+                a.PAGE_SET,                  
+                a.PHC_CD,                    
+                a.DATA_TYPE,                 
+                a.FIELD_SIZE, 
+                a.INVESTIGATION_KEY, 
+                a.INVESTIGATION_LOCAL_ID, 
+                a.PROGRAM_JURISDICTION_OID,
+                a.PATIENT_KEY,
+                a.PATIENT_LOCAL_ID,
+                a.DISEASE_NAME,
+                CASE 
+                    WHEN DATALENGTH(REPLACE(a.CONDITION_CD, ' ', '')) > 1 
+                    THEN a.CONDITION_CD
+                    ELSE a.PHC_CD
+                END AS DISEASE_CD,
+                CASE 
+                    WHEN DATALENGTH(a.page_set) < 2 
+                    THEN a.page_set
+                    ELSE a.page_set
+                END AS DISEASE_NM,
+                CASE 
+                    WHEN DATALENGTH(b.DATAMART_COLUMN_NM) > 2 
+                    THEN b.DATAMART_COLUMN_NM
+                    ELSE a.DATAMART_COLUMN_NM
+                END AS DATAMART_COLUMN_NM
+            INTO #ALL_TETANUS
+            FROM #LINKED_TETANUS a
+            FULL OUTER JOIN [dbo].LDF_DATAMART_COLUMN_REF b WITH(NOLOCK)
+                ON a.LDF_UID = b.LDF_UID
+            WHERE 
+                b.LDF_PAGE_SET = 'OTHER'
+                OR b.CONDITION_CD IN (
+                    SELECT CONDITION_CD 
+                    FROM [dbo].LDF_DATAMART_TABLE_REF WITH(NOLOCK)
+                    WHERE DATAMART_NAME = 'LDF_TETANUS'
+            )
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_SHORT_COL';
+            
+            IF OBJECT_ID('#ALL_TETANUS_SHORT_COL', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_SHORT_COL;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_SHORT_COL 
+            FROM #ALL_TETANUS 
+            WHERE data_type IN ('CV', 'ST');
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_SHORT_COL;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_TA';
+            
+            IF OBJECT_ID('#ALL_TETANUS_TA', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_TA;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_TA
+            FROM #ALL_TETANUS
+            WHERE data_type IN ('LIST_ST');  
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_TA;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_tetanus_ta;
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_TA);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_TA
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_TA';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_TA table same as #ALL_TETANUS_TA
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_TA';
+                END;
+
+            EXEC sp_executesql @sql_code;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_ta + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_tetanus_short_col;
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_SHORT_COL);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_SHORT_COL
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_SHORT_COL
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM #ALL_TETANUS_SHORT_COL';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_SHORT_COL table same as #ALL_TETANUS_SHORT_COL
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD                 
+                    INTO ' + @global_temp_tetanus_short_col +'
+                    FROM #ALL_TETANUS_SHORT_COL';
+                END;
+
+            BEGIN TRY
+                EXEC sp_executesql @sql_code;
+            END TRY
+            BEGIN CATCH
+                DECLARE @ErrorNumber1 INT = ERROR_NUMBER();
+                DECLARE @ErrorLine1 INT = ERROR_LINE();
+                DECLARE @ErrorMessage1 NVARCHAR(4000) = ERROR_MESSAGE();
+
+                IF @ErrorNumber1=511
+                    BEGIN
+                        -- process when error 511
+                        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+                        BEGIN
+                            DROP TABLE ' + @global_temp_tetanus_short_col +';
+                        END;')
+
+                        SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                        FROM (
+                            SELECT DISTINCT TOP 300 DATAMART_COLUMN_NM 
+                            FROM #ALL_TETANUS_SHORT_COL
+                        ) AS tmp;
+
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+
+                        EXEC sp_executesql @sql_code;
+
+                    END
+                    ELSE
+                    THROW @ErrorNumber1, @ErrorMessage1, @ErrorMessage1;
+            END CATCH
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_short_col + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_tetanus;
+
+            EXECUTE  [dbo].[sp_MERGE_TABLES] 
+                @INPUT_TABLE1= @global_temp_tetanus_short_col
+                ,@INPUT_TABLE2= @global_temp_tetanus_ta
+                ,@OUTPUT_TABLE= @global_temp_tetanus
+                ,@JOIN_ON_COLUMN='INVESTIGATION_KEY'
+                ,@batch_id= @batch_id
+                ,@target_table_name= @global_temp_tetanus;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            SET @sql_code = 'DELETE FROM ' + @global_temp_tetanus + ' WHERE INVESTIGATION_KEY IS NULL';
+            EXEC sp_executesql @sql_code;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #MISSED_COLS';
+
+            IF OBJECT_ID('#MISSED_COLS') IS NOT NULL
+                DROP TABLE #MISSED_COLS;
+
+            SELECT 
+                c1.COLUMN_NAME as col_nm,
+                'varchar' AS col_data_type, 
+                NULL AS col_NUMERIC_PRECISION, 
+                NULL AS col_NUMERIC_SCALE, 
+                2000 AS col_CHARACTER_MAXIMUM_LENGTH
+            INTO #MISSED_COLS    
+            FROM 
+                tempdb.INFORMATION_SCHEMA.COLUMNS c1
+            WHERE 
+                c1.TABLE_NAME = @global_temp_tetanus
+                AND c1.COLUMN_NAME NOT IN (
+                    SELECT c2.COLUMN_NAME 
+                    FROM INFORMATION_SCHEMA.COLUMNS c2 
+                    WHERE c2.TABLE_SCHEMA = 'dbo' 
+                    AND c2.TABLE_NAME = 'LDF_TETANUS'
+                )
+            ORDER BY 
+                c1.COLUMN_NAME;    
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                SELECT @Proc_Step_Name AS step, *
+                FROM #MISSED_COLS;
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            BEGIN TRANSACTION
+
+                SET
+                    @PROC_STEP_NO = @PROC_STEP_NO + 1;
+                SET
+                    @PROC_STEP_NAME = 'ADDING NEW COLUMNS TO TABLE LDF_TETANUS';
+                
+
+                set @sql_code = 'ALTER TABLE dbo.LDF_TETANUS ADD ' + (SELECT STRING_AGG( '[' + col_nm + '] ' +  col_data_type +
+                CASE
+                    WHEN col_data_type IN ('decimal', 'numeric') THEN '(' + CAST(col_NUMERIC_PRECISION AS NVARCHAR) + ',' + CAST(col_NUMERIC_SCALE AS NVARCHAR) + ')'
+                    WHEN col_data_type = 'varchar' THEN '(' +
+                        CASE WHEN col_CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX' ELSE CAST(col_CHARACTER_MAXIMUM_LENGTH AS NVARCHAR) END
+                    + ')'
+                    ELSE ''
+                END, ', ') FROM #MISSED_COLS);
+
+                EXEC sp_executesql @sql_code;
+                
+                SELECT @RowCount_no = @@ROWCOUNT;
+
+                IF
+                    @debug = 'true'
+                    SELECT @Proc_Step_Name AS step, col_nm
+                    FROM #MISSED_COLS;
+
+                INSERT INTO [dbo].[job_flow_log]
+                    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+                VALUES 
+                    (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+        
+            COMMIT TRANSACTION;    
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #COL_LIST TO INSERT';
+
+            IF OBJECT_ID('#COL_LIST') IS NOT NULL
+                DROP TABLE #COL_LIST;
+
+            SELECT DISTINCT ic.COLUMN_NAME, ORDINAL_POSITION
+            INTO #COL_LIST
+            FROM tempdb.INFORMATION_SCHEMA.COLUMNS ic 
+            WHERE ic.table_name = @global_temp_tetanus
+            AND UPPER(ic.COLUMN_NAME) NOT IN (
+                'INVESTIGATION_KEY',
+                'INVESTIGATION_LOCAL_ID',
+                'PROGRAM_JURISDICTION_OID',
+                'PATIENT_KEY',
+                'PATIENT_LOCAL_ID',
+                'DISEASE_NAME',
+                'DISEASE_CD'
+            ); --IGNORING DEFAULT COLUMNS;
+
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                SELECT @Proc_Step_Name AS step, *
+                FROM #COL_LIST;
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES (
+                @batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+            
+            
+            BEGIN TRANSACTION
+            
+                SET
+                    @PROC_STEP_NO = @PROC_STEP_NO + 1;
+                SET
+                    @PROC_STEP_NAME = 'DELETING INCOMING RECORDS FROM LDF_TETANUS';
+
+                IF OBJECT_ID('#LDF_PHC_UID_DEL') IS NOT NULL
+                    DROP TABLE #LDF_PHC_UID_DEL;
+                
+                SELECT DISTINCT L.INVESTIGATION_KEY 
+                INTO #LDF_PHC_UID_DEL
+                FROM #LDF_PHC_UID_LIST L 
+                INNER JOIN [dbo].LDF_TETANUS D 
+                    ON D.INVESTIGATION_KEY = L.INVESTIGATION_KEY
+                
+                DELETE D
+                FROM [dbo].LDF_TETANUS D 
+                INNER JOIN #LDF_PHC_UID_DEL T 
+                    ON T.INVESTIGATION_KEY = D.INVESTIGATION_KEY 
+
+                SELECT @RowCount_no = @@ROWCOUNT;
+
+                IF
+                    @debug = 'true'
+                    SELECT @Proc_Step_Name AS step, *
+                    FROM #LDF_PHC_UID_DEL;
+
+
+                INSERT INTO [dbo].[job_flow_log]
+                    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+                VALUES 
+                    (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            COMMIT TRANSACTION; 
+
+            ------------------------------------------------------------------------------------------------------------------------------------------   
+            
+
+            BEGIN TRANSACTION
+
+                SET
+                    @PROC_STEP_NO = @PROC_STEP_NO + 1;
+                SET
+                    @PROC_STEP_NAME = 'INSERTING INCOMING RECORDS TO LDF_TETANUS';
+                
+                
+                SELECT @ldf_columns = COALESCE(STRING_AGG(CAST(QUOTENAME(COLUMN_NAME) AS NVARCHAR(MAX)), ',') WITHIN GROUP (ORDER BY ORDINAL_POSITION), '')
+                FROM (SELECT DISTINCT COLUMN_NAME, ORDINAL_POSITION FROM #COL_LIST) AS cols;
+
+
+                SET @sql_code = 
+                'INSERT INTO dbo.LDF_TETANUS(
+                    INVESTIGATION_KEY,
+                    INVESTIGATION_LOCAL_ID,
+                    PROGRAM_JURISDICTION_OID,
+                    PATIENT_KEY,
+                    PATIENT_LOCAL_ID,
+                    DISEASE_NAME,
+                    DISEASE_CD' + 
+                    CASE
+                        WHEN @ldf_columns != '' THEN ',' + @ldf_columns
+                        ELSE ''
+                    END
+                + ' )
+                SELECT
+                    INVESTIGATION_KEY,
+                    INVESTIGATION_LOCAL_ID,
+                    PROGRAM_JURISDICTION_OID,
+                    PATIENT_KEY,
+                    PATIENT_LOCAL_ID,
+                    DISEASE_NAME,
+                    DISEASE_CD'+
+                + CASE
+                        WHEN @ldf_columns != '' THEN ',' + @ldf_columns
+                        ELSE ''
+                    END
+                +
+                '
+                FROM ' + @global_temp_tetanus;
+
+                EXEC sp_executesql @sql_code;
+
+                SELECT @RowCount_no = @@ROWCOUNT;
+
+                IF
+                    @debug = 'true'
+                    EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus + ';');
+
+                INSERT INTO [dbo].[job_flow_log]
+                    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+                VALUES 
+                    (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            COMMIT TRANSACTION;
+            
+
+        END
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+
+        SET @Proc_Step_no = 999;
+        SET @Proc_Step_Name = 'SP_COMPLETE';
+        SELECT @ROWCOUNT_NO = 0;
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus +';
+            END;')    
+
+        INSERT INTO [dbo].[job_flow_log] 
+        (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+        VALUES (@batch_id, @Dataflow_Name, @Package_Name, 'COMPLETE', 999, @Proc_Step_name, @RowCount_no);
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+    END TRY
+
+	BEGIN CATCH
+
+		IF @@TRANCOUNT > 0   ROLLBACK TRANSACTION;
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus +';
+            END;')
+
+		-- Construct the error message string with all details:
+			DECLARE @FullErrorMessage VARCHAR(8000) =
+				'Error Number: ' + CAST(ERROR_NUMBER() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +  -- Carriage return and line feed for new lines
+				'Error Severity: ' + CAST(ERROR_SEVERITY() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +
+				'Error State: ' + CAST(ERROR_STATE() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +
+				'Error Line: ' + CAST(ERROR_LINE() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +
+				'Error Message: ' + ERROR_MESSAGE();
+
+
+			INSERT INTO [dbo].[job_flow_log] 
+			    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [Error_Description], [row_count])
+			VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'ERROR', @Proc_Step_no, @Proc_Step_name, @FullErrorMessage, 0);
+
+		return -1 ;
+
+	END CATCH
+
+END;
diff --git a/db/upgrade/rdb_modern/tables/241-create_ldf_tetanus.sql b/db/upgrade/rdb_modern/tables/241-create_ldf_tetanus.sql
new file mode 100644
index 000000000..8443f30cb
--- /dev/null
+++ b/db/upgrade/rdb_modern/tables/241-create_ldf_tetanus.sql
@@ -0,0 +1,12 @@
+IF NOT EXISTS (SELECT 1 FROM sysobjects WHERE name = 'LDF_TETANUS' and xtype = 'U')
+BEGIN
+    CREATE TABLE [dbo].[LDF_TETANUS](
+        [INVESTIGATION_KEY] [numeric](20, 0) NULL,
+        [INVESTIGATION_LOCAL_ID] [varchar](50) NULL,
+        [PROGRAM_JURISDICTION_OID] [numeric](20, 0) NULL,
+        [PATIENT_KEY] [numeric](20, 0) NULL,
+        [PATIENT_LOCAL_ID] [varchar](50) NULL,
+        [DISEASE_NAME] [varchar](50) NULL,
+        [DISEASE_CD] [varchar](10) NULL
+    ) ON [PRIMARY];
+END
\ No newline at end of file
diff --git a/liquibase-service/src/main/resources/db/changelog/db.rdb_modern.changelog-16.1.yaml b/liquibase-service/src/main/resources/db/changelog/db.rdb_modern.changelog-16.1.yaml
index 8b3f0c6b1..38914bb65 100644
--- a/liquibase-service/src/main/resources/db/changelog/db.rdb_modern.changelog-16.1.yaml
+++ b/liquibase-service/src/main/resources/db/changelog/db.rdb_modern.changelog-16.1.yaml
@@ -2488,5 +2488,21 @@ databaseChangeLog:
       changes:
         - sqlFile:
             path: 290-sp_ldf_foodborne_datamart_postprocessing-001.sql
+            splitStatements: false 
+  - changeSet:
+      id: 810
+      author: liquibase
+      runOnChange: true
+      changes:
+        - sqlFile:
+            path: 241-create_ldf_tetanus-001.sql
+            splitStatements: false
+  - changeSet:
+      id: 815
+      author: liquibase
+      runOnChange: true
+      changes:
+        - sqlFile:
+            path: 300-sp_ldf_tetanus_datamart_postprocessing-001.sql
             splitStatements: false             
     
diff --git a/liquibase-service/src/main/resources/db/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing-001.sql b/liquibase-service/src/main/resources/db/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing-001.sql
index 1e5ce483a..341ef5b58 100644
--- a/liquibase-service/src/main/resources/db/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing-001.sql
+++ b/liquibase-service/src/main/resources/db/rdb_modern/routines/290-sp_ldf_foodborne_datamart_postprocessing-001.sql
@@ -185,7 +185,7 @@ BEGIN
                 ON b.INVESTIGATION_UID = i.CASE_UID 
             INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
                 ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
-            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
+            INNER JOIN [dbo].V_CONDITION_DIM c WITH (NOLOCK)
                 ON c.CONDITION_KEY = g.CONDITION_KEY
             INNER JOIN [dbo].D_PATIENT p WITH (NOLOCK)
                 ON p.PATIENT_KEY = g.PATIENT_KEY;
@@ -354,7 +354,7 @@ BEGIN
             SET
                 @PROC_STEP_NO = @PROC_STEP_NO + 1;
             SET
-                @PROC_STEP_NAME = 'GENERATING ##FOODBORNE_TA';
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_foodborne_ta;
             
             EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_foodborne_ta +''', ''U'')  IS NOT NULL
             BEGIN
@@ -449,7 +449,7 @@ BEGIN
             SET
                 @PROC_STEP_NO = @PROC_STEP_NO + 1;
             SET
-                @PROC_STEP_NAME = 'GENERATING ##FOODBORNE_SHORT_COL';
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_foodborne_short_col;
             
             EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_foodborne_short_col +''', ''U'')  IS NOT NULL
             BEGIN
@@ -507,7 +507,7 @@ BEGIN
                             PATIENT_LOCAL_ID,
                             DISEASE_NAME,
                             DISEASE_CD
-                        INTO ' + @global_temp_foodborne_ta +'
+                        INTO ' + @global_temp_foodborne_short_col +'
                         FROM #ALL_FOODBORNE_SHORT_COL';
                     END
                 END
@@ -521,7 +521,7 @@ BEGIN
                         PATIENT_LOCAL_ID,
                         DISEASE_NAME,
                         DISEASE_CD                 
-                    INTO ' + @global_temp_foodborne_ta +'
+                    INTO ' + @global_temp_foodborne_short_col +'
                     FROM #ALL_FOODBORNE_SHORT_COL';
                 END;
 
@@ -598,7 +598,7 @@ BEGIN
             SET
                 @PROC_STEP_NO = @PROC_STEP_NO + 1;
             SET
-                @PROC_STEP_NAME = 'GENERATING ##FOODBORNE';
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_foodborne;
 
             EXECUTE  [dbo].[sp_MERGE_TABLES] 
                 @INPUT_TABLE1= @global_temp_foodborne_short_col
diff --git a/liquibase-service/src/main/resources/db/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing-001.sql b/liquibase-service/src/main/resources/db/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing-001.sql
new file mode 100644
index 000000000..cf9e9639b
--- /dev/null
+++ b/liquibase-service/src/main/resources/db/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing-001.sql
@@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].V_CONDITION_DIM c WITH (NOLOCK)
+                ON c.CONDITION_KEY = g.CONDITION_KEY
+            INNER JOIN [dbo].D_PATIENT p WITH (NOLOCK)
+                ON p.PATIENT_KEY = g.PATIENT_KEY;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #LINKED_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+            
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS';
+            
+            IF OBJECT_ID('#ALL_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS;
+
+            SELECT 
+                a.COL1,
+                a.INVESTIGATION_UID,          
+                a.CODE_SHORT_DESC_TXT,     
+                a.LDF_UID,                       
+                a.CODE_SET_NM,             
+                a.LABEL_TXT,               
+                a.DATA_SOURCE,              
+                a.CDC_NATIONAL_ID,          
+                a.BUSINESS_OBJECT_NM,       
+                a.CONDITION_CD,              
+                a.CUSTOM_SUBFORM_METADATA_UID,     
+                a.PAGE_SET,                  
+                a.PHC_CD,                    
+                a.DATA_TYPE,                 
+                a.FIELD_SIZE, 
+                a.INVESTIGATION_KEY, 
+                a.INVESTIGATION_LOCAL_ID, 
+                a.PROGRAM_JURISDICTION_OID,
+                a.PATIENT_KEY,
+                a.PATIENT_LOCAL_ID,
+                a.DISEASE_NAME,
+                CASE 
+                    WHEN DATALENGTH(REPLACE(a.CONDITION_CD, ' ', '')) > 1 
+                    THEN a.CONDITION_CD
+                    ELSE a.PHC_CD
+                END AS DISEASE_CD,
+                CASE 
+                    WHEN DATALENGTH(a.page_set) < 2 
+                    THEN a.page_set
+                    ELSE a.page_set
+                END AS DISEASE_NM,
+                CASE 
+                    WHEN DATALENGTH(b.DATAMART_COLUMN_NM) > 2 
+                    THEN b.DATAMART_COLUMN_NM
+                    ELSE a.DATAMART_COLUMN_NM
+                END AS DATAMART_COLUMN_NM
+            INTO #ALL_TETANUS
+            FROM #LINKED_TETANUS a
+            FULL OUTER JOIN [dbo].LDF_DATAMART_COLUMN_REF b WITH(NOLOCK)
+                ON a.LDF_UID = b.LDF_UID
+            WHERE 
+                b.LDF_PAGE_SET = 'OTHER'
+                OR b.CONDITION_CD IN (
+                    SELECT CONDITION_CD 
+                    FROM [dbo].LDF_DATAMART_TABLE_REF WITH(NOLOCK)
+                    WHERE DATAMART_NAME = 'LDF_TETANUS'
+            )
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_SHORT_COL';
+            
+            IF OBJECT_ID('#ALL_TETANUS_SHORT_COL', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_SHORT_COL;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_SHORT_COL 
+            FROM #ALL_TETANUS 
+            WHERE data_type IN ('CV', 'ST');
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_SHORT_COL;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_TA';
+            
+            IF OBJECT_ID('#ALL_TETANUS_TA', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_TA;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_TA
+            FROM #ALL_TETANUS
+            WHERE data_type IN ('LIST_ST');  
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_TA;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_tetanus_ta;
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_TA);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_TA
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_TA';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_TA table same as #ALL_TETANUS_TA
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_TA';
+                END;
+
+            EXEC sp_executesql @sql_code;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_ta + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_tetanus_short_col;
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_SHORT_COL);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_SHORT_COL
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_SHORT_COL
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM #ALL_TETANUS_SHORT_COL';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_SHORT_COL table same as #ALL_TETANUS_SHORT_COL
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD                 
+                    INTO ' + @global_temp_tetanus_short_col +'
+                    FROM #ALL_TETANUS_SHORT_COL';
+                END;
+
+            BEGIN TRY
+                EXEC sp_executesql @sql_code;
+            END TRY
+            BEGIN CATCH
+                DECLARE @ErrorNumber1 INT = ERROR_NUMBER();
+                DECLARE @ErrorLine1 INT = ERROR_LINE();
+                DECLARE @ErrorMessage1 NVARCHAR(4000) = ERROR_MESSAGE();
+
+                IF @ErrorNumber1=511
+                    BEGIN
+                        -- process when error 511
+                        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+                        BEGIN
+                            DROP TABLE ' + @global_temp_tetanus_short_col +';
+                        END;')
+
+                        SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                        FROM (
+                            SELECT DISTINCT TOP 300 DATAMART_COLUMN_NM 
+                            FROM #ALL_TETANUS_SHORT_COL
+                        ) AS tmp;
+
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+
+                        EXEC sp_executesql @sql_code;
+
+                    END
+                    ELSE
+                    THROW @ErrorNumber1, @ErrorMessage1, @ErrorMessage1;
+            END CATCH
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_short_col + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ' + @global_temp_tetanus;
+
+            EXECUTE  [dbo].[sp_MERGE_TABLES] 
+                @INPUT_TABLE1= @global_temp_tetanus_short_col
+                ,@INPUT_TABLE2= @global_temp_tetanus_ta
+                ,@OUTPUT_TABLE= @global_temp_tetanus
+                ,@JOIN_ON_COLUMN='INVESTIGATION_KEY'
+                ,@batch_id= @batch_id
+                ,@target_table_name= @global_temp_tetanus;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            SET @sql_code = 'DELETE FROM ' + @global_temp_tetanus + ' WHERE INVESTIGATION_KEY IS NULL';
+            EXEC sp_executesql @sql_code;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #MISSED_COLS';
+
+            IF OBJECT_ID('#MISSED_COLS') IS NOT NULL
+                DROP TABLE #MISSED_COLS;
+
+            SELECT 
+                c1.COLUMN_NAME as col_nm,
+                'varchar' AS col_data_type, 
+                NULL AS col_NUMERIC_PRECISION, 
+                NULL AS col_NUMERIC_SCALE, 
+                2000 AS col_CHARACTER_MAXIMUM_LENGTH
+            INTO #MISSED_COLS    
+            FROM 
+                tempdb.INFORMATION_SCHEMA.COLUMNS c1
+            WHERE 
+                c1.TABLE_NAME = @global_temp_tetanus
+                AND c1.COLUMN_NAME NOT IN (
+                    SELECT c2.COLUMN_NAME 
+                    FROM INFORMATION_SCHEMA.COLUMNS c2 
+                    WHERE c2.TABLE_SCHEMA = 'dbo' 
+                    AND c2.TABLE_NAME = 'LDF_TETANUS'
+                )
+            ORDER BY 
+                c1.COLUMN_NAME;    
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                SELECT @Proc_Step_Name AS step, *
+                FROM #MISSED_COLS;
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            BEGIN TRANSACTION
+
+                SET
+                    @PROC_STEP_NO = @PROC_STEP_NO + 1;
+                SET
+                    @PROC_STEP_NAME = 'ADDING NEW COLUMNS TO TABLE LDF_TETANUS';
+                
+
+                set @sql_code = 'ALTER TABLE dbo.LDF_TETANUS ADD ' + (SELECT STRING_AGG( '[' + col_nm + '] ' +  col_data_type +
+                CASE
+                    WHEN col_data_type IN ('decimal', 'numeric') THEN '(' + CAST(col_NUMERIC_PRECISION AS NVARCHAR) + ',' + CAST(col_NUMERIC_SCALE AS NVARCHAR) + ')'
+                    WHEN col_data_type = 'varchar' THEN '(' +
+                        CASE WHEN col_CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX' ELSE CAST(col_CHARACTER_MAXIMUM_LENGTH AS NVARCHAR) END
+                    + ')'
+                    ELSE ''
+                END, ', ') FROM #MISSED_COLS);
+
+                EXEC sp_executesql @sql_code;
+                
+                SELECT @RowCount_no = @@ROWCOUNT;
+
+                IF
+                    @debug = 'true'
+                    SELECT @Proc_Step_Name AS step, col_nm
+                    FROM #MISSED_COLS;
+
+                INSERT INTO [dbo].[job_flow_log]
+                    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+                VALUES 
+                    (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+        
+            COMMIT TRANSACTION;    
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #COL_LIST TO INSERT';
+
+            IF OBJECT_ID('#COL_LIST') IS NOT NULL
+                DROP TABLE #COL_LIST;
+
+            SELECT DISTINCT ic.COLUMN_NAME, ORDINAL_POSITION
+            INTO #COL_LIST
+            FROM tempdb.INFORMATION_SCHEMA.COLUMNS ic 
+            WHERE ic.table_name = @global_temp_tetanus
+            AND UPPER(ic.COLUMN_NAME) NOT IN (
+                'INVESTIGATION_KEY',
+                'INVESTIGATION_LOCAL_ID',
+                'PROGRAM_JURISDICTION_OID',
+                'PATIENT_KEY',
+                'PATIENT_LOCAL_ID',
+                'DISEASE_NAME',
+                'DISEASE_CD'
+            ); --IGNORING DEFAULT COLUMNS;
+
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                SELECT @Proc_Step_Name AS step, *
+                FROM #COL_LIST;
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES (
+                @batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+            
+            
+            BEGIN TRANSACTION
+            
+                SET
+                    @PROC_STEP_NO = @PROC_STEP_NO + 1;
+                SET
+                    @PROC_STEP_NAME = 'DELETING INCOMING RECORDS FROM LDF_TETANUS';
+
+                IF OBJECT_ID('#LDF_PHC_UID_DEL') IS NOT NULL
+                    DROP TABLE #LDF_PHC_UID_DEL;
+                
+                SELECT DISTINCT L.INVESTIGATION_KEY 
+                INTO #LDF_PHC_UID_DEL
+                FROM #LDF_PHC_UID_LIST L 
+                INNER JOIN [dbo].LDF_TETANUS D 
+                    ON D.INVESTIGATION_KEY = L.INVESTIGATION_KEY
+                
+                DELETE D
+                FROM [dbo].LDF_TETANUS D 
+                INNER JOIN #LDF_PHC_UID_DEL T 
+                    ON T.INVESTIGATION_KEY = D.INVESTIGATION_KEY 
+
+                SELECT @RowCount_no = @@ROWCOUNT;
+
+                IF
+                    @debug = 'true'
+                    SELECT @Proc_Step_Name AS step, *
+                    FROM #LDF_PHC_UID_DEL;
+
+
+                INSERT INTO [dbo].[job_flow_log]
+                    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+                VALUES 
+                    (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            COMMIT TRANSACTION; 
+
+            ------------------------------------------------------------------------------------------------------------------------------------------   
+            
+
+            BEGIN TRANSACTION
+
+                SET
+                    @PROC_STEP_NO = @PROC_STEP_NO + 1;
+                SET
+                    @PROC_STEP_NAME = 'INSERTING INCOMING RECORDS TO LDF_TETANUS';
+                
+                
+                SELECT @ldf_columns = COALESCE(STRING_AGG(CAST(QUOTENAME(COLUMN_NAME) AS NVARCHAR(MAX)), ',') WITHIN GROUP (ORDER BY ORDINAL_POSITION), '')
+                FROM (SELECT DISTINCT COLUMN_NAME, ORDINAL_POSITION FROM #COL_LIST) AS cols;
+
+
+                SET @sql_code = 
+                'INSERT INTO dbo.LDF_TETANUS(
+                    INVESTIGATION_KEY,
+                    INVESTIGATION_LOCAL_ID,
+                    PROGRAM_JURISDICTION_OID,
+                    PATIENT_KEY,
+                    PATIENT_LOCAL_ID,
+                    DISEASE_NAME,
+                    DISEASE_CD' + 
+                    CASE
+                        WHEN @ldf_columns != '' THEN ',' + @ldf_columns
+                        ELSE ''
+                    END
+                + ' )
+                SELECT
+                    INVESTIGATION_KEY,
+                    INVESTIGATION_LOCAL_ID,
+                    PROGRAM_JURISDICTION_OID,
+                    PATIENT_KEY,
+                    PATIENT_LOCAL_ID,
+                    DISEASE_NAME,
+                    DISEASE_CD'+
+                + CASE
+                        WHEN @ldf_columns != '' THEN ',' + @ldf_columns
+                        ELSE ''
+                    END
+                +
+                '
+                FROM ' + @global_temp_tetanus;
+
+                EXEC sp_executesql @sql_code;
+
+                SELECT @RowCount_no = @@ROWCOUNT;
+
+                IF
+                    @debug = 'true'
+                    EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus + ';');
+
+                INSERT INTO [dbo].[job_flow_log]
+                    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+                VALUES 
+                    (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            COMMIT TRANSACTION;
+            
+
+        END
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+
+        SET @Proc_Step_no = 999;
+        SET @Proc_Step_Name = 'SP_COMPLETE';
+        SELECT @ROWCOUNT_NO = 0;
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus +';
+            END;')    
+
+        INSERT INTO [dbo].[job_flow_log] 
+        (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+        VALUES (@batch_id, @Dataflow_Name, @Package_Name, 'COMPLETE', 999, @Proc_Step_name, @RowCount_no);
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+    END TRY
+
+	BEGIN CATCH
+
+		IF @@TRANCOUNT > 0   ROLLBACK TRANSACTION;
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus +';
+            END;')
+
+		-- Construct the error message string with all details:
+			DECLARE @FullErrorMessage VARCHAR(8000) =
+				'Error Number: ' + CAST(ERROR_NUMBER() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +  -- Carriage return and line feed for new lines
+				'Error Severity: ' + CAST(ERROR_SEVERITY() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +
+				'Error State: ' + CAST(ERROR_STATE() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +
+				'Error Line: ' + CAST(ERROR_LINE() AS VARCHAR(10)) + CHAR(13) + CHAR(10) +
+				'Error Message: ' + ERROR_MESSAGE();
+
+
+			INSERT INTO [dbo].[job_flow_log] 
+			    (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [Error_Description], [row_count])
+			VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'ERROR', @Proc_Step_no, @Proc_Step_name, @FullErrorMessage, 0);
+
+		return -1 ;
+
+	END CATCH
+
+END;
diff --git a/liquibase-service/src/main/resources/db/rdb_modern/tables/241-create_ldf_tetanus-001.sql b/liquibase-service/src/main/resources/db/rdb_modern/tables/241-create_ldf_tetanus-001.sql
new file mode 100644
index 000000000..8443f30cb
--- /dev/null
+++ b/liquibase-service/src/main/resources/db/rdb_modern/tables/241-create_ldf_tetanus-001.sql
@@ -0,0 +1,12 @@
+IF NOT EXISTS (SELECT 1 FROM sysobjects WHERE name = 'LDF_TETANUS' and xtype = 'U')
+BEGIN
+    CREATE TABLE [dbo].[LDF_TETANUS](
+        [INVESTIGATION_KEY] [numeric](20, 0) NULL,
+        [INVESTIGATION_LOCAL_ID] [varchar](50) NULL,
+        [PROGRAM_JURISDICTION_OID] [numeric](20, 0) NULL,
+        [PATIENT_KEY] [numeric](20, 0) NULL,
+        [PATIENT_LOCAL_ID] [varchar](50) NULL,
+        [DISEASE_NAME] [varchar](50) NULL,
+        [DISEASE_CD] [varchar](10) NULL
+    ) ON [PRIMARY];
+END
\ No newline at end of file
diff --git a/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/repository/InvestigationRepository.java b/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/repository/InvestigationRepository.java
index c181bce1f..337dd5fc0 100644
--- a/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/repository/InvestigationRepository.java
+++ b/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/repository/InvestigationRepository.java
@@ -149,4 +149,8 @@ List<DatamartData> executeStoredProcForPublicHealthCaseIds(
     
     @Procedure("sp_ldf_foodborne_datamart_postprocessing")
     void executeStoredProcForLdfFoodBorneDatamart(@Param("publicHealthCaseUids") String publicHealthCaseUids);
+    
+    @Procedure("sp_ldf_tetanus_datamart_postprocessing")
+    void executeStoredProcForLdfTetanusDatamart(@Param("publicHealthCaseUids") String publicHealthCaseUids);
+    
 }
diff --git a/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/Entity.java b/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/Entity.java
index cff8d1d10..b5de09d65 100644
--- a/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/Entity.java
+++ b/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/Entity.java
@@ -62,6 +62,7 @@ enum Entity {
     LDF_GENERIC(0, "LDF_GENERIC", Constants.PHC_UID, "sp_ldf_generic_datamart_postprocessing"),
     LDF_BMIRD(0, "LDF_BMIRD", Constants.PHC_UID, "sp_ldf_bmird_datamart_postprocessing"),
     LDF_FOODBORNE(0, "LDF_FOODBORNE", Constants.PHC_UID, "sp_ldf_foodborne_datamart_postprocessing"),
+    LDF_TETANUS(0, "LDF_TETANUS", Constants.PHC_UID, "sp_ldf_tetanus_datamart_postprocessing"),
     UNKNOWN(-1, "unknown", "unknown_uid", "sp_nrt_unknown_postprocessing"); 
 
     private final int priority;
diff --git a/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingService.java b/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingService.java
index b3275c591..db6a2952a 100644
--- a/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingService.java
+++ b/post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingService.java
@@ -546,7 +546,7 @@ protected void processDatamartIds() {
                 // for complex value (e.g. "GENERIC_CASE,LDF_GENERIC") the key should be parsed
                 String[] dmTypes = dmKey.split(",");
                 String dmType =  dmTypes[0];
-                String ldfType = (ldfEnable && dmTypes.length > 1) ? dmTypes[1] : "" ;
+                String ldfType = (ldfEnable && dmTypes.length > 1) ? dmTypes[1] : "UNKNOWN" ;
 
                 Queue<Long> uids = entry.getValue().get(INVESTIGATION.getEntityName());
                 dmCache.put(dmKey, new ConcurrentHashMap<>());
@@ -570,16 +570,28 @@ protected void processDatamartIds() {
                     case GENERIC_CASE:
                         executeDatamartProc(GENERIC_CASE,
                             investigationRepository::executeStoredProcForGenericCaseDatamart, cases);
-                            
-                            if(ldfType.equalsIgnoreCase("LDF_GENERIC")){
-                                executeDatamartProc(LDF_GENERIC,
-                                investigationRepository::executeStoredProcForLdfGenericDatamart, cases);
-                            
-                            }
-                            if(ldfType.equalsIgnoreCase("LDF_FOODBORNE")){
-                                executeDatamartProc(LDF_FOODBORNE,
-                                investigationRepository::executeStoredProcForLdfFoodBorneDatamart, cases);
+                        
+                        if (ldfEnable){
+                            switch (Entity.valueOf(ldfType.toUpperCase())) {
+                                case LDF_GENERIC:
+                                    executeDatamartProc(LDF_GENERIC,
+                                    investigationRepository::executeStoredProcForLdfGenericDatamart, cases);
+                                    break;
+                                case LDF_FOODBORNE:
+                                    executeDatamartProc(LDF_FOODBORNE,
+                                    investigationRepository::executeStoredProcForLdfFoodBorneDatamart, cases);
+                                    break;
+                                case LDF_TETANUS:
+                                    // to test this, Tetanus must be added as a legacy page and 
+                                    // [dbo].nrt_datamart_metadata table must be updated adding TETANUS
+                                    executeDatamartProc(LDF_TETANUS,
+                                    investigationRepository::executeStoredProcForLdfTetanusDatamart, cases);
+                                    break;
+                                default:
+                                    break;
                             }
+                        }    
+                        
                         break;
                     case CRS_CASE:
                         executeDatamartProc(CRS_CASE,
@@ -601,7 +613,7 @@ protected void processDatamartIds() {
                         executeDatamartProc(BMIRD_CASE,
                                 investigationRepository::executeStoredProcForBmirdCaseDatamart, cases);
                         
-                        if(ldfType.equalsIgnoreCase("LDF_BMIRD")){
+                        if(ldfEnable && ldfType.equalsIgnoreCase("LDF_BMIRD")){
                             executeDatamartProc(LDF_BMIRD,
                             investigationRepository::executeStoredProcForLdfBmirdDatamart, cases);
                         }
diff --git a/post-processing-service/src/test/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingServiceTest.java b/post-processing-service/src/test/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingServiceTest.java
index e7b334b1a..302d41ad8 100644
--- a/post-processing-service/src/test/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingServiceTest.java
+++ b/post-processing-service/src/test/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingServiceTest.java
@@ -698,6 +698,47 @@ void testPostProcessBmirdLDFCase() {
         assertTrue(logs.get(6).getFormattedMessage().contains(BMIRD_STREP_PNEUMO_DATAMART.getStoredProcedure()));
     }
 
+    @Test
+    void testPostProcessGenericCaseLdfTetanusNegative() {
+        
+        postProcessingServiceMock.setLdfEnable(false);
+        String topic = "dummy_datamart";
+        String msg = "{\"payload\":{\"public_health_case_uid\":123,\"patient_uid\":456,\"condition_cd\":\"12020\"," +
+                "\"datamart\":\"Generic_Case,LDF_TETANUS\",\"stored_procedure\":\"\"}}";
+
+        postProcessingServiceMock.postProcessDatamart(topic, msg);
+        postProcessingServiceMock.processDatamartIds();
+
+        String id = "123";
+        verify(investigationRepositoryMock).executeStoredProcForGenericCaseDatamart(id);
+
+        List<ILoggingEvent> logs = listAppender.list;
+        assertEquals(3, logs.size());
+        assertTrue(logs.get(2).getFormattedMessage().contains(GENERIC_CASE.getStoredProcedure()));
+    }
+
+    @Test
+    void testPostProcessGenericCaseLdfTetanus() {
+
+        postProcessingServiceMock.setLdfEnable(true);
+        String topic = "dummy_datamart";
+        String msg = "{\"payload\":{\"public_health_case_uid\":123,\"patient_uid\":456,\"condition_cd\":\"12020\"," +
+                "\"datamart\":\"Generic_Case,LDF_TETANUS\",\"stored_procedure\":\"\"}}";
+
+        postProcessingServiceMock.postProcessDatamart(topic, msg);
+        postProcessingServiceMock.processDatamartIds();
+
+        String id = "123";
+        verify(investigationRepositoryMock).executeStoredProcForGenericCaseDatamart(id);
+        verify(investigationRepositoryMock).executeStoredProcForLdfTetanusDatamart(id);
+
+        List<ILoggingEvent> logs = listAppender.list;
+        assertEquals(5, logs.size());
+        assertTrue(logs.get(2).getFormattedMessage().contains(GENERIC_CASE.getStoredProcedure()));
+        assertTrue(logs.get(4).getFormattedMessage().contains(LDF_TETANUS.getStoredProcedure()));
+    }
+
+
     @Test
     void testPostProcessTBDatamart() {
         String topic = "tb_datamart";
@@ -810,7 +851,7 @@ static Stream<DatamartTestCase> datamartTestData() {
                         verify(repo).executeStoredProcForGenericCaseDatamart(uid);
                         verify(repo).executeStoredProcForLdfFoodBorneDatamart(uid);
                         }
-                    ),    
+                    ),                       
                 new DatamartTestCase(
                         "{\"payload\":{\"public_health_case_uid\":123,\"patient_uid\":456,\"condition_cd\":\"10370\"," +
                                 "\"datamart\":\"CRS_Case\",\"stored_procedure\":\"sp_rubella_case_datamart_postprocessing\"}}",


Reviews:
user: login: upasanapattnaik-eq
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T17:53:41Z
user: login: joshua-mills-cdw
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T19:19:32Z
user: login: agonzalez-cloud39
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T20:00:09Z
user: login: joshua-mills-cdw
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T20:14:06Z
user: login: joshua-mills-cdw
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-23T20:14:43Z
user: login: agonzalez-cloud39
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-24T18:09:49Z
user: login: upasanapattnaik-eq
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-25T11:42:46Z
user: login: upasanapattnaik-eq
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-25T11:44:15Z
user: login: agonzalez-cloud39
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-25T16:15:44Z
user: login: upasanapattnaik-eq
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-25T16:23:06Z
user: login: agonzalez-cloud39
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-25T16:25:47Z
user: login: upasanapattnaik-eq
body: LGTM!
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-04-28T13:13:42Z

Review comments:
diff_hunk: @@ -576,6 +576,14 @@ protected void processDatamartIds() {
                                 investigationRepository::executeStoredProcForLdfGenericDatamart, cases);
                             
                             }
+                            if(ldfEnable && ldfType.equalsIgnoreCase("LDF_TETANUS")){
path: post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingService.java
user: login: upasanapattnaik-eq
body: 👍 
created_at: 2025-04-23T17:53:41Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing-001.sql
user: login: joshua-mills-cdw
body: Just a quick comment about the file name here, since this one is in the `db/upgrade` folder, the `-001` at the end should be removed
created_at: 2025-04-23T19:19:32Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing-001.sql
user: login: agonzalez-cloud39
body: yes, removed
created_at: 2025-04-23T20:00:09Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##LDF_TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##LDF_TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##LDF_TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
+                ON c.CONDITION_KEY = g.CONDITION_KEY
+            INNER JOIN [dbo].D_PATIENT p WITH (NOLOCK)
+                ON p.PATIENT_KEY = g.PATIENT_KEY;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #LINKED_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+            
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS';
+            
+            IF OBJECT_ID('#ALL_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS;
+
+            SELECT 
+                a.COL1,
+                a.INVESTIGATION_UID,          
+                a.CODE_SHORT_DESC_TXT,     
+                a.LDF_UID,                       
+                a.CODE_SET_NM,             
+                a.LABEL_TXT,               
+                a.DATA_SOURCE,              
+                a.CDC_NATIONAL_ID,          
+                a.BUSINESS_OBJECT_NM,       
+                a.CONDITION_CD,              
+                a.CUSTOM_SUBFORM_METADATA_UID,     
+                a.PAGE_SET,                  
+                a.PHC_CD,                    
+                a.DATA_TYPE,                 
+                a.FIELD_SIZE, 
+                a.INVESTIGATION_KEY, 
+                a.INVESTIGATION_LOCAL_ID, 
+                a.PROGRAM_JURISDICTION_OID,
+                a.PATIENT_KEY,
+                a.PATIENT_LOCAL_ID,
+                a.DISEASE_NAME,
+                CASE 
+                    WHEN DATALENGTH(REPLACE(a.CONDITION_CD, ' ', '')) > 1 
+                    THEN a.CONDITION_CD
+                    ELSE a.PHC_CD
+                END AS DISEASE_CD,
+                CASE 
+                    WHEN DATALENGTH(a.page_set) < 2 
+                    THEN a.page_set
+                    ELSE a.page_set
+                END AS DISEASE_NM,
+                CASE 
+                    WHEN DATALENGTH(b.DATAMART_COLUMN_NM) > 2 
+                    THEN b.DATAMART_COLUMN_NM
+                    ELSE a.DATAMART_COLUMN_NM
+                END AS DATAMART_COLUMN_NM
+            INTO #ALL_TETANUS
+            FROM #LINKED_TETANUS a
+            FULL OUTER JOIN [dbo].LDF_DATAMART_COLUMN_REF b WITH(NOLOCK)
+                ON a.LDF_UID = b.LDF_UID
+            WHERE 
+                b.LDF_PAGE_SET = 'OTHER'
+                OR b.CONDITION_CD IN (
+                    SELECT CONDITION_CD 
+                    FROM [dbo].LDF_DATAMART_TABLE_REF WITH(NOLOCK)
+                    WHERE DATAMART_NAME = 'LDF_TETANUS'
+            )
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_SHORT_COL';
+            
+            IF OBJECT_ID('#ALL_TETANUS_SHORT_COL', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_SHORT_COL;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_SHORT_COL 
+            FROM #ALL_TETANUS 
+            WHERE data_type IN ('CV', 'ST');
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_SHORT_COL;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_TA';
+            
+            IF OBJECT_ID('#ALL_TETANUS_TA', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_TA;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_TA
+            FROM #ALL_TETANUS
+            WHERE data_type IN ('LIST_ST');  
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_TA;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS_TA';
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_TA);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_TA
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_TA';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_TA table same as #ALL_TETANUS_TA
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_TA';
+                END;
+
+            EXEC sp_executesql @sql_code;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_ta + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS_SHORT_COL';
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_SHORT_COL);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_SHORT_COL
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_SHORT_COL
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_SHORT_COL';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_SHORT_COL table same as #ALL_TETANUS_SHORT_COL
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD                 
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_SHORT_COL';
+                END;
+
+            BEGIN TRY
+                EXEC sp_executesql @sql_code;
+            END TRY
+            BEGIN CATCH
+                DECLARE @ErrorNumber1 INT = ERROR_NUMBER();
+                DECLARE @ErrorLine1 INT = ERROR_LINE();
+                DECLARE @ErrorMessage1 NVARCHAR(4000) = ERROR_MESSAGE();
+
+                IF @ErrorNumber1=511
+                    BEGIN
+                        -- process when error 511
+                        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+                        BEGIN
+                            DROP TABLE ' + @global_temp_tetanus_short_col +';
+                        END;')
+
+                        SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                        FROM (
+                            SELECT DISTINCT TOP 300 DATAMART_COLUMN_NM 
+                            FROM #ALL_TETANUS_SHORT_COL
+                        ) AS tmp;
+
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+
+                        EXEC sp_executesql @sql_code;
+
+                    END
+                    ELSE
+                    THROW @ErrorNumber1, @ErrorMessage1, @ErrorMessage1;
+            END CATCH
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_short_col + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS';
+
+            EXECUTE  [dbo].[sp_MERGE_TABLES] 
+                @INPUT_TABLE1= @global_temp_tetanus_short_col
+                ,@INPUT_TABLE2= @global_temp_tetanus_ta
+                ,@OUTPUT_TABLE= @global_temp_tetanus
+                ,@JOIN_ON_COLUMN='INVESTIGATION_KEY'
+                ,@batch_id= @batch_id
+                ,@target_table_name= @global_temp_tetanus;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            SET @sql_code = 'DELETE FROM ' + @global_temp_tetanus + ' WHERE INVESTIGATION_KEY IS NULL';
+            EXEC sp_executesql @sql_code;
+
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
user: login: joshua-mills-cdw
body: About line 601, for debugging purposes - is this table ##LDF_TETANUS with batch_id appended (as in the @global_temp_tetanus variable), or will it just be ##TETANUS ? Might be a good idea to edit the step name to match the name of the table being created if it's not
created_at: 2025-04-23T20:14:06Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##LDF_TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##LDF_TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##LDF_TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
+                ON c.CONDITION_KEY = g.CONDITION_KEY
+            INNER JOIN [dbo].D_PATIENT p WITH (NOLOCK)
+                ON p.PATIENT_KEY = g.PATIENT_KEY;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #LINKED_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+            
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS';
+            
+            IF OBJECT_ID('#ALL_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS;
+
+            SELECT 
+                a.COL1,
+                a.INVESTIGATION_UID,          
+                a.CODE_SHORT_DESC_TXT,     
+                a.LDF_UID,                       
+                a.CODE_SET_NM,             
+                a.LABEL_TXT,               
+                a.DATA_SOURCE,              
+                a.CDC_NATIONAL_ID,          
+                a.BUSINESS_OBJECT_NM,       
+                a.CONDITION_CD,              
+                a.CUSTOM_SUBFORM_METADATA_UID,     
+                a.PAGE_SET,                  
+                a.PHC_CD,                    
+                a.DATA_TYPE,                 
+                a.FIELD_SIZE, 
+                a.INVESTIGATION_KEY, 
+                a.INVESTIGATION_LOCAL_ID, 
+                a.PROGRAM_JURISDICTION_OID,
+                a.PATIENT_KEY,
+                a.PATIENT_LOCAL_ID,
+                a.DISEASE_NAME,
+                CASE 
+                    WHEN DATALENGTH(REPLACE(a.CONDITION_CD, ' ', '')) > 1 
+                    THEN a.CONDITION_CD
+                    ELSE a.PHC_CD
+                END AS DISEASE_CD,
+                CASE 
+                    WHEN DATALENGTH(a.page_set) < 2 
+                    THEN a.page_set
+                    ELSE a.page_set
+                END AS DISEASE_NM,
+                CASE 
+                    WHEN DATALENGTH(b.DATAMART_COLUMN_NM) > 2 
+                    THEN b.DATAMART_COLUMN_NM
+                    ELSE a.DATAMART_COLUMN_NM
+                END AS DATAMART_COLUMN_NM
+            INTO #ALL_TETANUS
+            FROM #LINKED_TETANUS a
+            FULL OUTER JOIN [dbo].LDF_DATAMART_COLUMN_REF b WITH(NOLOCK)
+                ON a.LDF_UID = b.LDF_UID
+            WHERE 
+                b.LDF_PAGE_SET = 'OTHER'
+                OR b.CONDITION_CD IN (
+                    SELECT CONDITION_CD 
+                    FROM [dbo].LDF_DATAMART_TABLE_REF WITH(NOLOCK)
+                    WHERE DATAMART_NAME = 'LDF_TETANUS'
+            )
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_SHORT_COL';
+            
+            IF OBJECT_ID('#ALL_TETANUS_SHORT_COL', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_SHORT_COL;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_SHORT_COL 
+            FROM #ALL_TETANUS 
+            WHERE data_type IN ('CV', 'ST');
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_SHORT_COL;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_TA';
+            
+            IF OBJECT_ID('#ALL_TETANUS_TA', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_TA;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_TA
+            FROM #ALL_TETANUS
+            WHERE data_type IN ('LIST_ST');  
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_TA;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS_TA';
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_TA);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_TA
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_TA';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_TA table same as #ALL_TETANUS_TA
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_TA';
+                END;
+
+            EXEC sp_executesql @sql_code;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_ta + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS_SHORT_COL';
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_SHORT_COL);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_SHORT_COL
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_SHORT_COL
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_SHORT_COL';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_SHORT_COL table same as #ALL_TETANUS_SHORT_COL
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD                 
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_SHORT_COL';
+                END;
+
+            BEGIN TRY
+                EXEC sp_executesql @sql_code;
+            END TRY
+            BEGIN CATCH
+                DECLARE @ErrorNumber1 INT = ERROR_NUMBER();
+                DECLARE @ErrorLine1 INT = ERROR_LINE();
+                DECLARE @ErrorMessage1 NVARCHAR(4000) = ERROR_MESSAGE();
+
+                IF @ErrorNumber1=511
+                    BEGIN
+                        -- process when error 511
+                        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+                        BEGIN
+                            DROP TABLE ' + @global_temp_tetanus_short_col +';
+                        END;')
+
+                        SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                        FROM (
+                            SELECT DISTINCT TOP 300 DATAMART_COLUMN_NM 
+                            FROM #ALL_TETANUS_SHORT_COL
+                        ) AS tmp;
+
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+
+                        EXEC sp_executesql @sql_code;
+
+                    END
+                    ELSE
+                    THROW @ErrorNumber1, @ErrorMessage1, @ErrorMessage1;
+            END CATCH
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_short_col + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS';
+
+            EXECUTE  [dbo].[sp_MERGE_TABLES] 
+                @INPUT_TABLE1= @global_temp_tetanus_short_col
+                ,@INPUT_TABLE2= @global_temp_tetanus_ta
+                ,@OUTPUT_TABLE= @global_temp_tetanus
+                ,@JOIN_ON_COLUMN='INVESTIGATION_KEY'
+                ,@batch_id= @batch_id
+                ,@target_table_name= @global_temp_tetanus;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            SET @sql_code = 'DELETE FROM ' + @global_temp_tetanus + ' WHERE INVESTIGATION_KEY IS NULL';
+            EXEC sp_executesql @sql_code;
+
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
user: login: joshua-mills-cdw
body: Probably not necessary to have the batch_id appended into the step name, though
created_at: 2025-04-23T20:14:43Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##LDF_TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##LDF_TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##LDF_TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
+                ON c.CONDITION_KEY = g.CONDITION_KEY
+            INNER JOIN [dbo].D_PATIENT p WITH (NOLOCK)
+                ON p.PATIENT_KEY = g.PATIENT_KEY;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #LINKED_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+            
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS';
+            
+            IF OBJECT_ID('#ALL_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS;
+
+            SELECT 
+                a.COL1,
+                a.INVESTIGATION_UID,          
+                a.CODE_SHORT_DESC_TXT,     
+                a.LDF_UID,                       
+                a.CODE_SET_NM,             
+                a.LABEL_TXT,               
+                a.DATA_SOURCE,              
+                a.CDC_NATIONAL_ID,          
+                a.BUSINESS_OBJECT_NM,       
+                a.CONDITION_CD,              
+                a.CUSTOM_SUBFORM_METADATA_UID,     
+                a.PAGE_SET,                  
+                a.PHC_CD,                    
+                a.DATA_TYPE,                 
+                a.FIELD_SIZE, 
+                a.INVESTIGATION_KEY, 
+                a.INVESTIGATION_LOCAL_ID, 
+                a.PROGRAM_JURISDICTION_OID,
+                a.PATIENT_KEY,
+                a.PATIENT_LOCAL_ID,
+                a.DISEASE_NAME,
+                CASE 
+                    WHEN DATALENGTH(REPLACE(a.CONDITION_CD, ' ', '')) > 1 
+                    THEN a.CONDITION_CD
+                    ELSE a.PHC_CD
+                END AS DISEASE_CD,
+                CASE 
+                    WHEN DATALENGTH(a.page_set) < 2 
+                    THEN a.page_set
+                    ELSE a.page_set
+                END AS DISEASE_NM,
+                CASE 
+                    WHEN DATALENGTH(b.DATAMART_COLUMN_NM) > 2 
+                    THEN b.DATAMART_COLUMN_NM
+                    ELSE a.DATAMART_COLUMN_NM
+                END AS DATAMART_COLUMN_NM
+            INTO #ALL_TETANUS
+            FROM #LINKED_TETANUS a
+            FULL OUTER JOIN [dbo].LDF_DATAMART_COLUMN_REF b WITH(NOLOCK)
+                ON a.LDF_UID = b.LDF_UID
+            WHERE 
+                b.LDF_PAGE_SET = 'OTHER'
+                OR b.CONDITION_CD IN (
+                    SELECT CONDITION_CD 
+                    FROM [dbo].LDF_DATAMART_TABLE_REF WITH(NOLOCK)
+                    WHERE DATAMART_NAME = 'LDF_TETANUS'
+            )
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_SHORT_COL';
+            
+            IF OBJECT_ID('#ALL_TETANUS_SHORT_COL', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_SHORT_COL;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_SHORT_COL 
+            FROM #ALL_TETANUS 
+            WHERE data_type IN ('CV', 'ST');
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_SHORT_COL;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #ALL_TETANUS_TA';
+            
+            IF OBJECT_ID('#ALL_TETANUS_TA', 'U') IS NOT NULL
+			    DROP TABLE #ALL_TETANUS_TA;
+
+            SELECT 
+                INVESTIGATION_KEY,
+                INVESTIGATION_LOCAL_ID,
+                PROGRAM_JURISDICTION_OID,
+                PATIENT_KEY,
+                PATIENT_LOCAL_ID,
+                DISEASE_NAME,
+                DISEASE_CD,
+                DATAMART_COLUMN_NM,
+                col1  
+            INTO #ALL_TETANUS_TA
+            FROM #ALL_TETANUS
+            WHERE data_type IN ('LIST_ST');  
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #ALL_TETANUS_TA;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS_TA';
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_ta +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_ta +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_TA);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_TA
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_TA';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_TA table same as #ALL_TETANUS_TA
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_TA';
+                END;
+
+            EXEC sp_executesql @sql_code;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_ta + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS_SHORT_COL';
+            
+            EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+            BEGIN
+                DROP TABLE ' + @global_temp_tetanus_short_col +';
+            END;')
+
+            SET @count = (SELECT count(*) FROM #ALL_TETANUS_SHORT_COL);
+
+            IF @count > 0
+                BEGIN
+                    -- Build the comma-separated list of quoted DATAMART_COLUMN_NM values
+                    SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                    FROM (
+                        SELECT DISTINCT DATAMART_COLUMN_NM 
+                        FROM #ALL_TETANUS_SHORT_COL
+                    ) AS tmp;
+
+                    IF @ldf_columns != ''
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_SHORT_COL
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+                    END
+                    ELSE
+                    BEGIN
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD
+                        INTO ' + @global_temp_tetanus_ta +'
+                        FROM #ALL_TETANUS_SHORT_COL';
+                    END
+                END
+                ELSE -- If data does not exist create GLOBAL_TEMP_TETANUS_SHORT_COL table same as #ALL_TETANUS_SHORT_COL
+                BEGIN    
+                    SET @sql_code = 'SELECT 
+                        INVESTIGATION_KEY,
+                        INVESTIGATION_LOCAL_ID,
+                        PROGRAM_JURISDICTION_OID,
+                        PATIENT_KEY,
+                        PATIENT_LOCAL_ID,
+                        DISEASE_NAME,
+                        DISEASE_CD                 
+                    INTO ' + @global_temp_tetanus_ta +'
+                    FROM #ALL_TETANUS_SHORT_COL';
+                END;
+
+            BEGIN TRY
+                EXEC sp_executesql @sql_code;
+            END TRY
+            BEGIN CATCH
+                DECLARE @ErrorNumber1 INT = ERROR_NUMBER();
+                DECLARE @ErrorLine1 INT = ERROR_LINE();
+                DECLARE @ErrorMessage1 NVARCHAR(4000) = ERROR_MESSAGE();
+
+                IF @ErrorNumber1=511
+                    BEGIN
+                        -- process when error 511
+                        EXEC ('IF OBJECT_ID(''tempdb..' + @global_temp_tetanus_short_col +''', ''U'')  IS NOT NULL
+                        BEGIN
+                            DROP TABLE ' + @global_temp_tetanus_short_col +';
+                        END;')
+
+                        SELECT @ldf_columns = STRING_AGG(QUOTENAME(DATAMART_COLUMN_NM), ',')
+                        FROM (
+                            SELECT DISTINCT TOP 300 DATAMART_COLUMN_NM 
+                            FROM #ALL_TETANUS_SHORT_COL
+                        ) AS tmp;
+
+                        SET @sql_code = 'SELECT 
+                            INVESTIGATION_KEY,
+                            INVESTIGATION_LOCAL_ID,
+                            PROGRAM_JURISDICTION_OID,
+                            PATIENT_KEY,
+                            PATIENT_LOCAL_ID,
+                            DISEASE_NAME,
+                            DISEASE_CD,
+                            ' + @ldf_columns + '
+                        INTO ' + @global_temp_tetanus_short_col +'
+                        FROM (
+                            SELECT 
+                                INVESTIGATION_KEY,
+                                INVESTIGATION_LOCAL_ID,
+                                PROGRAM_JURISDICTION_OID,
+                                PATIENT_KEY,
+                                PATIENT_LOCAL_ID,
+                                DISEASE_NAME,
+                                DISEASE_CD,
+                                DATAMART_COLUMN_NM,
+                                LEFT(COL1, 8000) AS ANSWERCOL 
+                            FROM #ALL_TETANUS_TA
+                            ) AS SourceTable
+                        PIVOT (
+                            MAX(ANSWERCOL)
+                            FOR DATAMART_COLUMN_NM IN (' + @ldf_columns + ')
+                        ) AS PivotTable';
+
+                        EXEC sp_executesql @sql_code;
+
+                    END
+                    ELSE
+                    THROW @ErrorNumber1, @ErrorMessage1, @ErrorMessage1;
+            END CATCH
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            IF
+                @debug = 'true'
+                EXEC('SELECT '''+ @Proc_Step_Name +''' AS step, * FROM ' + @global_temp_tetanus_short_col + ';');
+
+            INSERT INTO [dbo].[job_flow_log]
+                (batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+            VALUES 
+                (@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+            ------------------------------------------------------------------------------------------------------------------------------------------    
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING ##TETANUS';
+
+            EXECUTE  [dbo].[sp_MERGE_TABLES] 
+                @INPUT_TABLE1= @global_temp_tetanus_short_col
+                ,@INPUT_TABLE2= @global_temp_tetanus_ta
+                ,@OUTPUT_TABLE= @global_temp_tetanus
+                ,@JOIN_ON_COLUMN='INVESTIGATION_KEY'
+                ,@batch_id= @batch_id
+                ,@target_table_name= @global_temp_tetanus;
+
+            SELECT @RowCount_no = @@ROWCOUNT;
+
+            SET @sql_code = 'DELETE FROM ' + @global_temp_tetanus + ' WHERE INVESTIGATION_KEY IS NULL';
+            EXEC sp_executesql @sql_code;
+
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
user: login: agonzalez-cloud39
body: I like the idea, I did add the batch_id in debug
created_at: 2025-04-24T18:09:48Z
author_association: CONTRIBUTOR
diff_hunk: @@ -571,15 +571,25 @@ protected void processDatamartIds() {
                         executeDatamartProc(GENERIC_CASE,
                             investigationRepository::executeStoredProcForGenericCaseDatamart, cases);
                             
path: post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingService.java
user: login: upasanapattnaik-eq
body: Can we include a feature flag to the LDFs till they're all added in? 
created_at: 2025-04-25T11:42:46Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
user: login: upasanapattnaik-eq
body: Ah small change here: Usage of `v_condition_dim` view instead of the `Condition` table 
created_at: 2025-04-25T11:44:15Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
user: login: agonzalez-cloud39
body: In Foodborne_Datamart and Vaccine_Prevent_Diseases_Datamart we are also using the CONDITION table. Do you want me to modify those sp as well  right? 
created_at: 2025-04-25T16:15:44Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,911 @@
+
+CREATE OR ALTER PROCEDURE [dbo].[sp_ldf_tetanus_datamart_postprocessing]  
+  @phc_id_list nvarchar(max),
+  @debug bit = 'false'
+ AS
+BEGIN
+
+	DECLARE @batch_id BIGINT;
+    SET @batch_id = cast((format(getdate(),'yyMMddHHmmssffff')) AS BIGINT);
+    PRINT @batch_id;
+    DECLARE @RowCount_no INT =0;
+    DECLARE @Proc_Step_no FLOAT = 0; 
+    DECLARE @Proc_Step_Name VARCHAR(200) = '';
+	DECLARE @Dataflow_Name VARCHAR(200) = 'LDF_TETANUS POST-Processing';
+	DECLARE @Package_Name VARCHAR(200) = 'sp_ldf_tetanus_datamart_postprocessing';
+
+    DECLARE @global_temp_tetanus_ta varchar(500) = '';
+    DECLARE @global_temp_tetanus_short_col varchar(500) = '';
+    DECLARE @global_temp_tetanus varchar(500) = '';
+    DECLARE @sql_code NVARCHAR(MAX);
+    DECLARE @ldf_columns NVARCHAR(MAX) = '';
+    DECLARE @count BIGINT;
+
+    SET @global_temp_tetanus_ta = '##TETANUS_TA' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus_short_col = '##TETANUS_SHORT_COL' + '_' + CAST(@Batch_id as varchar(50)); 
+    SET @global_temp_tetanus = '##TETANUS' + '_' + CAST(@Batch_id as varchar(50)); 
+    
+ 
+	BEGIN TRY
+    
+		SET @Proc_Step_Name = 'SP_Start';
+		
+		INSERT INTO dbo.job_flow_log ( batch_id
+                                    , [Dataflow_Name]
+                                    , [package_Name]
+                                    , [Status_Type]
+                                    , [step_number]
+                                    , [step_name]
+                                    , [row_count]
+                                    , [Msg_Description1])
+        VALUES ( @batch_id
+            , @Dataflow_Name
+            , @Package_Name
+            , 'START'
+            , @Proc_Step_no
+            , @Proc_Step_Name
+            , 0
+            , LEFT('ID List-' + @phc_id_list, 500));
+
+
+		------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET
+			@PROC_STEP_NO = @PROC_STEP_NO + 1;
+		SET
+			@PROC_STEP_NAME = 'GENERATING #LDF_PHC_UID_LIST TABLE';
+
+		IF OBJECT_ID('#LDF_PHC_UID_LIST', 'U') IS NOT NULL
+			DROP TABLE #LDF_PHC_UID_LIST;
+
+		SELECT 
+			s.INVESTIGATION_UID,
+			s.LDF_UID,
+            inv.INVESTIGATION_KEY
+		INTO  #LDF_PHC_UID_LIST
+		FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK) 
+        INNER JOIN [dbo].INVESTIGATION inv WITH (NOLOCK) 
+            ON inv.CASE_UID = s.INVESTIGATION_UID
+		INNER JOIN (SELECT TRIM(value) AS value FROM STRING_SPLIT(@phc_id_list, ',')) nu 
+            ON nu.value = s.INVESTIGATION_UID 
+			
+
+		SELECT @RowCount_no = @@ROWCOUNT;
+
+		IF
+			@debug = 'true'
+			SELECT @Proc_Step_Name AS step, *
+			FROM #LDF_PHC_UID_LIST;
+
+		INSERT INTO [dbo].[job_flow_log]
+			(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+		VALUES 	
+			(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no);
+
+
+        ------------------------------------------------------------------------------------------------------------------------------------------
+
+        SET @count =
+        (
+            SELECT COUNT(1)
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.condition_cd AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID
+        );	
+            
+        IF (@count > 0)
+
+        BEGIN
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #BASE_TETANUS';
+            
+            IF OBJECT_ID('#BASE_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #BASE_TETANUS;
+
+            SELECT DISTINCT
+                s.COL1,
+                s.INVESTIGATION_UID,          
+                s.CODE_SHORT_DESC_TXT,     
+                s.LDF_UID,                       
+                s.CODE_SET_NM,             
+                s.LABEL_TXT,               
+                s.DATA_SOURCE,              
+                s.CDC_NATIONAL_ID,          
+                s.BUSINESS_OBJECT_NM,       
+                s.CONDITION_CD,              
+                s.CUSTOM_SUBFORM_METADATA_UID,     
+                s.PAGE_SET,                  
+                s.DATAMART_COLUMN_NM,       
+                s.PHC_CD,                    
+                s.DATA_TYPE,                 
+                s.FIELD_SIZE
+            INTO #BASE_TETANUS
+            FROM [dbo].LDF_DIMENSIONAL_DATA s WITH (NOLOCK)
+            INNER JOIN [dbo].LDF_DATAMART_TABLE_REF r WITH (NOLOCK) 
+                ON s.PHC_CD = r.CONDITION_CD AND r.DATAMART_NAME = 'LDF_TETANUS'
+            INNER JOIN #LDF_PHC_UID_LIST l  
+                ON l.investigation_uid = s.INVESTIGATION_UID;
+
+            SELECT @ROWCOUNT_NO = @@ROWCOUNT;
+
+			IF
+				@debug = 'true'
+				SELECT @Proc_Step_Name AS step, *
+				FROM #BASE_TETANUS;
+
+			INSERT INTO [dbo].[job_flow_log]
+				(batch_id, [Dataflow_Name], [package_Name], [Status_Type], [step_number], [step_name], [row_count])
+			VALUES 
+				(@batch_id, @Dataflow_Name, @Package_Name, 'START', @Proc_Step_no, @Proc_Step_Name, @RowCount_no); 
+
+
+            ------------------------------------------------------------------------------------------------------------------------------------------
+        
+            SET
+                @PROC_STEP_NO = @PROC_STEP_NO + 1;
+            SET
+                @PROC_STEP_NAME = 'GENERATING #LINKED_TETANUS';
+            
+            IF OBJECT_ID('#LINKED_TETANUS', 'U') IS NOT NULL
+			    DROP TABLE #LINKED_TETANUS;
+
+            SELECT 
+                b.COL1,
+                b.INVESTIGATION_UID,          
+                b.CODE_SHORT_DESC_TXT,     
+                b.LDF_UID,                       
+                b.CODE_SET_NM,             
+                b.LABEL_TXT,               
+                b.DATA_SOURCE,              
+                b.CDC_NATIONAL_ID,          
+                b.BUSINESS_OBJECT_NM,       
+                b.CONDITION_CD,              
+                b.CUSTOM_SUBFORM_METADATA_UID,     
+                b.PAGE_SET,                  
+                b.DATAMART_COLUMN_NM,       
+                b.PHC_CD,                    
+                b.DATA_TYPE,                 
+                b.FIELD_SIZE, 
+                i.INVESTIGATION_KEY, 
+                i.INV_LOCAL_ID  AS INVESTIGATION_LOCAL_ID, 
+                i.CASE_OID  AS PROGRAM_JURISDICTION_OID,
+                g.PATIENT_KEY,
+                p.PATIENT_LOCAL_ID,
+                c.CONDITION_SHORT_NM AS DISEASE_NAME
+            INTO #LINKED_TETANUS
+            FROM #BASE_TETANUS b
+            INNER JOIN [dbo].INVESTIGATION i WITH (NOLOCK)
+                ON b.INVESTIGATION_UID = i.CASE_UID 
+            INNER JOIN [dbo].GENERIC_CASE g WITH (NOLOCK)
+                ON g.INVESTIGATION_KEY = i.INVESTIGATION_KEY
+            INNER JOIN [dbo].CONDITION c WITH (NOLOCK)
path: db/upgrade/rdb_modern/routines/300-sp_ldf_tetanus_datamart_postprocessing.sql
user: login: upasanapattnaik-eq
body: Yes, please. Did not notice it earlier. 
Thank you!
created_at: 2025-04-25T16:23:06Z
author_association: CONTRIBUTOR
diff_hunk: @@ -571,15 +571,25 @@ protected void processDatamartIds() {
                         executeDatamartProc(GENERIC_CASE,
                             investigationRepository::executeStoredProcForGenericCaseDatamart, cases);
                             
path: post-processing-service/src/main/java/gov/cdc/etldatapipeline/postprocessingservice/service/PostProcessingService.java
user: login: agonzalez-cloud39
body: Switch to execute LDFs wrapped with if (ldfEnable) {} (line 574)
Also, ldfEnable added in condition to execute LDF_BMIRD (line 616)
created_at: 2025-04-25T16:25:47Z
author_association: CONTRIBUTOR

PR comments:
user: login: sonarqubecloud[bot]
created_at: 2025-04-25T19:55:42Z
body: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=CDCgov_NEDSS-DataReporting&pullRequest=376) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=CDCgov_NEDSS-DataReporting&pullRequest=376&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=CDCgov_NEDSS-DataReporting&pullRequest=376&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=CDCgov_NEDSS-DataReporting&pullRequest=376&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=CDCgov_NEDSS-DataReporting&pullRequest=376&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=CDCgov_NEDSS-DataReporting&pullRequest=376&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=CDCgov_NEDSS-DataReporting&pullRequest=376)


author_association: NONE