|
| 1 | +{{#each dataObjectMappings}} |
| 2 | + |
| 3 | +-- |
| 4 | +-- PSA DIRECT proces for {{targetDataObject.name}} using a DIRECT wrapper. |
| 5 | +-- |
| 6 | + |
| 7 | +-- |
| 8 | +-- Persistent Staging Area Delta Detection Procedure for {{targetDataObject.name}} |
| 9 | +-- |
| 10 | + |
| 11 | +IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'SP_{{targetDataObject.name}}') AND type in (N'P')) |
| 12 | +DROP PROCEDURE [SP_{{targetDataObject.name}}]; |
| 13 | +GO |
| 14 | + |
| 15 | +CREATE PROCEDURE [SP_{{targetDataObject.name}}] @{{../metadataConfiguration.etlProcessAttribute}} INT |
| 16 | +AS |
| 17 | + |
| 18 | +WITH STG_CTE AS |
| 19 | +( |
| 20 | +SELECT |
| 21 | + {{#each dataItemMappings}} |
| 22 | + [{{sourceDataItems.0.name}}] AS [{{targetDataItem.name}}], |
| 23 | + {{/each}} |
| 24 | + HASHBYTES('MD5', |
| 25 | + {{#each dataItemMappings}} |
| 26 | + ISNULL(RTRIM(CONVERT(NVARCHAR(100),[{{sourceDataItems.0.name}}])), 'N/A') + '#~!'{{#unless @last}} +{{/unless}} |
| 27 | + {{/each}} |
| 28 | + ) AS [{{../metadataConfiguration.recordChecksumAttribute}}] |
| 29 | +FROM [{{sourceDataObjects.0.dataObjectConnection.extensions.0.value}}].[{{sourceDataObjects.0.dataObjectConnection.extensions.1.value}}].[{{sourceDataObjects.0.name}}] |
| 30 | +), |
| 31 | +PSA_CTE AS |
| 32 | +( |
| 33 | +SELECT |
| 34 | + A.[{{../metadataConfiguration.recordChecksumAttribute}}] AS [{{../metadataConfiguration.recordChecksumAttribute}}], |
| 35 | + {{#each dataItemMappings}} |
| 36 | + A.[{{sourceDataItems.0.name}}] AS [{{targetDataItem.name}}]{{#unless @last}},{{/unless}} |
| 37 | + {{/each}} |
| 38 | +FROM [{{targetDataObject.dataObjectConnection.extensions.0.value}}].[{{targetDataObject.dataObjectConnection.extensions.1.value}}].[{{targetDataObject.name}}] A |
| 39 | +JOIN |
| 40 | + ( |
| 41 | + SELECT {{#each businessKeys}} {{#each businessKeyComponentMapping}} |
| 42 | + [{{sourceDataItems.0.name}}], {{/each}} {{/each}} |
| 43 | + MAX({{../metadataConfiguration.loadDateTimeAttribute}}) AS MAX_{{../metadataConfiguration.loadDateTimeAttribute}} |
| 44 | + FROM [{{targetDataObject.dataObjectConnection.extensions.0.value}}].[{{targetDataObject.dataObjectConnection.extensions.1.value}}].[{{targetDataObject.name}}] A |
| 45 | + GROUP BY {{#each businessKeys}} {{#each businessKeyComponentMapping}} |
| 46 | + [{{sourceDataItems.0.name}}] {{#unless @last}},{{/unless}}{{/each}} {{/each}} |
| 47 | + ) B ON {{#each businessKeys}} {{#each businessKeyComponentMapping}} |
| 48 | + A.[{{sourceDataItems.0.name}}] = B.[{{sourceDataItems.0.name}}] AND {{/each}} {{/each}} |
| 49 | + A.{{../metadataConfiguration.loadDateTimeAttribute}} = B.MAX_{{../metadataConfiguration.loadDateTimeAttribute}} |
| 50 | + WHERE {{../metadataConfiguration.changeDataCaptureAttribute}} != 'D' |
| 51 | +) |
| 52 | +INSERT INTO [{{targetDataObject.dataObjectConnection.extensions.0.value}}].[{{targetDataObject.dataObjectConnection.extensions.1.value}}].[{{targetDataObject.name}}] |
| 53 | +( |
| 54 | + [{{../metadataConfiguration.loadDateTimeAttribute}}], |
| 55 | + [{{../metadataConfiguration.sourceRowIdAttribute}}], |
| 56 | + [{{../metadataConfiguration.eventDateTimeAttribute}}], |
| 57 | + [{{../metadataConfiguration.changeDataCaptureAttribute}}], |
| 58 | + [{{../metadataConfiguration.etlProcessAttribute}}], |
| 59 | + [{{../metadataConfiguration.recordChecksumAttribute}}], |
| 60 | + {{#each dataItemMappings}} |
| 61 | + [{{sourceDataItems.0.name}}]{{#unless @last}},{{/unless}} |
| 62 | + {{/each}} |
| 63 | +) |
| 64 | +SELECT |
| 65 | + -- Framework columns |
| 66 | + SYSDATETIME() [{{../metadataConfiguration.loadDateTimeAttribute}}], |
| 67 | + ROW_NUMBER() OVER |
| 68 | + (ORDER BY |
| 69 | + {{#each businessKeys}}{{#each businessKeyComponentMapping}}CASE WHEN STG_CTE.[{{sourceDataItems.0.name}}] IS NULL THEN PSA_CTE.[{{sourceDataItems.0.name}}] ELSE STG_CTE.[{{sourceDataItems.0.name}}] END{{#unless @last}},{{/unless}}{{/each}}{{/each}} |
| 70 | + ) AS [{{../metadataConfiguration.sourceRowIdAttribute}}], |
| 71 | + SYSDATETIME() [{{../metadataConfiguration.eventDateTimeAttribute}}], |
| 72 | + CASE |
| 73 | + WHEN STG_CTE.{{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}[{{sourceDataItems.0.name}}]{{/if}}{{/each}}{{/each}} IS NULL THEN 'D' |
| 74 | + WHEN PSA_CTE.{{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}[{{sourceDataItems.0.name}}]{{/if}}{{/each}}{{/each}} IS NULL THEN 'C' --Inserts are also C |
| 75 | + WHEN STG_CTE.{{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}[{{sourceDataItems.0.name}}]{{/if}}{{/each}}{{/each}} IS NOT NULL |
| 76 | + AND PSA_CTE.{{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}[{{sourceDataItems.0.name}}]{{/if}}{{/each}}{{/each}} IS NOT NULL |
| 77 | + AND STG_CTE.[{{../metadataConfiguration.recordChecksumAttribute}}] != PSA_CTE.[{{../metadataConfiguration.recordChecksumAttribute}}] THEN 'C' ELSE 'No Change' |
| 78 | + END AS {{../metadataConfiguration.changeDataCaptureAttribute}}, |
| 79 | + @AUDIT_TRAIL_ID AS [{{../metadataConfiguration.etlProcessAttribute}}], |
| 80 | + CASE |
| 81 | + WHEN STG_CTE.{{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}[{{sourceDataItems.0.name}}]{{/if}}{{/each}}{{/each}} IS NULL |
| 82 | + THEN PSA_CTE.[{{../metadataConfiguration.recordChecksumAttribute}}] |
| 83 | + ELSE STG_CTE.[{{../metadataConfiguration.recordChecksumAttribute}}] |
| 84 | + END AS [{{../metadataConfiguration.recordChecksumAttribute}}], |
| 85 | + -- Regular columns |
| 86 | + {{#each dataItemMappings}} |
| 87 | + CASE |
| 88 | + WHEN STG_CTE.{{#each ../businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}[{{sourceDataItems.0.name}}]{{/if}}{{/each}}{{/each}} IS NULL |
| 89 | + THEN PSA_CTE.[{{sourceDataItems.0.name}}] |
| 90 | + ELSE STG_CTE.[{{sourceDataItems.0.name}}] |
| 91 | + END AS [{{sourceDataItems.0.name}}]{{#unless @last}},{{/unless}}{{/each}} |
| 92 | +FROM STG_CTE |
| 93 | +FULL OUTER JOIN PSA_CTE ON {{#each businessKeys}} {{#each businessKeyComponentMapping}} |
| 94 | + PSA_CTE.[{{sourceDataItems.0.name}}] = STG_CTE.[{{sourceDataItems.0.name}}]{{#unless @last}} AND{{/unless}}{{/each}} {{/each}} |
| 95 | +WHERE |
| 96 | +( |
| 97 | + CASE |
| 98 | + WHEN {{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}STG_CTE.[{{sourceDataItems.0.name}}] IS NULL THEN 'D'{{/if}}{{/each}}{{/each}} |
| 99 | + WHEN {{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}PSA_CTE.[{{sourceDataItems.0.name}}] IS NULL THEN 'C'{{/if}}{{/each}}{{/each}} |
| 100 | + WHEN {{#each businessKeys}}{{#each businessKeyComponentMapping}}{{#if @first}}PSA_CTE.[{{sourceDataItems.0.name}}] IS NOT NULL AND PSA_CTE.{{sourceDataItems.0.name}} IS NOT NULL AND STG_CTE.[{{../../../metadataConfiguration.recordChecksumAttribute}}] != PSA_CTE.[{{../../../metadataConfiguration.recordChecksumAttribute}}] THEN 'C'{{/if}}{{/each}}{{/each}} |
| 101 | + ELSE 'No Change' |
| 102 | + END |
| 103 | +) != 'No Change' |
| 104 | + |
| 105 | +SELECT @@ROWCOUNT AS ROWS_INSERTED |
| 106 | +GO |
| 107 | + |
| 108 | +EXEC [900_Direct_Framework].[omd].[RegisterModule] |
| 109 | + @ModuleCode = '150_{{targetDataObject.name}}' |
| 110 | +,@ModuleAreaCode = 'PSA' |
| 111 | +,@Executable = 'EXEC [{{targetDataObject.dataObjectConnection.extensions.0.value}}].[{{targetDataObject.dataObjectConnection.extensions.1.value}}].[SP_{{targetDataObject.name}}] @AUDIT_TRAIL_ID = @ModuleInstanceId' |
| 112 | +,@ModuleDescription = 'Persistent Staging Area process for [{{sourceDataObjects.0.name}}]' |
| 113 | +,@ModuleSourceDataObject = '[{{sourceDataObjects.0.dataObjectConnection.extensions.0.value}}].[{{sourceDataObjects.0.dataObjectConnection.extensions.1.value}}].[{{sourceDataObjects.0.name}}]' |
| 114 | +,@ModuleTargetDataObject = '[{{targetDataObject.dataObjectConnection.extensions.0.value}}].[{{targetDataObject.dataObjectConnection.extensions.1.value}}].[{{targetDataObject.name}}]' |
| 115 | + |
| 116 | +-- Run the process |
| 117 | +EXEC [900_Direct_Framework].[omd].[RunModule] @ModuleCode = '150_{{targetDataObject.name}}', @Debug='Y', @ModuleInstanceIdColumnName='AUDIT_TRAIL_ID' |
| 118 | +{{/each}} |
0 commit comments