|
| 1 | +{{#each dataObjectMappings}} |
| 2 | +-- |
| 3 | +-- Link Insert Into statement for {{targetDataObject.name}} |
| 4 | +-- |
| 5 | + |
| 6 | +IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'INT.SP_{{targetDataObject.name}}_{{sourceDataObjects.0.name}}') AND type in (N'P')) |
| 7 | +DROP PROCEDURE [INT].[SP_{{targetDataObject.name}}_{{sourceDataObjects.0.name}}]; |
| 8 | +GO |
| 9 | + |
| 10 | +CREATE PROCEDURE [INT].[SP_{{targetDataObject.name}}_{{sourceDataObjects.0.name}}] @OMD_INSERT_MODULE_INSTANCE_ID int |
| 11 | +AS |
| 12 | + |
| 13 | +-- |
| 14 | +-- LNK Insert Into statement for [{{targetDataObject.name}}] |
| 15 | +-- Includes the GROUP BY clause |
| 16 | + |
| 17 | +INSERT INTO [INT].[{{targetDataObject.name}}] |
| 18 | +( {{#each businessKeys}} {{#if @first}} [{{surrogateKey}}], {{/if}}{{/each}} |
| 19 | + {{#each businessKeys}}{{#unless @first}} |
| 20 | + [{{surrogateKey}}],{{/unless}} {{/each}} |
| 21 | + OMD_INSERT_DATETIME, |
| 22 | + OMD_RECORD_SOURCE_ID, |
| 23 | + OMD_INSERT_MODULE_INSTANCE_ID |
| 24 | +) |
| 25 | + |
| 26 | +SELECT |
| 27 | + CONVERT(CHAR,HASHBYTES('SHA1',UPPER( {{#each businessKeys}} {{#unless @first}}{{!-- Create the Surrogate Key using the combined Business Keys --}} |
| 28 | +{{#each dataItemMappings}} RTRIM(ISNULL([{{targetDataItem.name}}], 'NA')) + '|'+{{/each}} |
| 29 | +{{#each businessKeyComponentMapping}} RTRIM(ISNULL([{{targetDataItem.name}}{{@../index }}], 'NA')) {{#unless @last}} + '|' +{{/unless}}{{/each}}{{#unless @last}} +'|'+{{/unless}}{{/unless}}{{/each}} |
| 30 | +{{#each dataItemMappings}} + RTRIM(ISNULL([{{targetDataItem.name}}], 'NA')) + '|'+{{/each}}{{#each businessKeys}} {{#if @first}})),2) AS [{{surrogateKey}}],{{/if}}{{/each}} |
| 31 | + {{#each businessKeys}}{{#unless @first}} {{!-- List out the separate (Hub) business keys, by skipping the first generic relationship key. --}} |
| 32 | + {{#unless businessKeyClassification}} |
| 33 | + CONVERT(CHAR,HASHBYTES('SHA1',UPPER( {{#each businessKeyComponentMapping}} RTRIM(ISNULL([{{targetDataItem.name}}{{@../index }}],'NA')) {{#unless @last}} +'|'+{{/unless}}{{/each}})),2) AS [{{surrogateKey}}], |
| 34 | + {{/unless}}{{#if businessKeyClassification}}{{#each businessKeyComponentMapping}}[{{targetDataItem.name}}{{@../index }}]{{/each}} AS [{{surrogateKey}}], |
| 35 | + {{/if}} {{/unless}} {{/each}}{{#each dataItemMappings}} |
| 36 | + [{{targetDataItem.name}}],{{/each}} |
| 37 | + OMD_INSERT_DATETIME, |
| 38 | + OMD_RECORD_SOURCE_ID, |
| 39 | + @OMD_INSERT_MODULE_INSTANCE_ID |
| 40 | +FROM |
| 41 | +( |
| 42 | + SELECT {{#each businessKeys}} {{#unless @first}} {{#each businessKeyComponentMapping}} |
| 43 | + CAST({{stringcompare sourceDataItems.0.isHardCodedValue 'True'}}[{{/stringcompare}}{{sourceDataItems.0.name}}{{stringcompare sourceDataItems.0.isHardCodedValue 'True'}}]{{/stringcompare}} AS NVARCHAR(100)) AS [{{targetDataItem.name}}{{@../index }}],{{/each}}{{/unless}}{{/each}}{{#each dataItemMappings}} |
| 44 | + [{{sourceDataItems.0.name}}] AS [{{targetDataItem.name}}],{{/each}} |
| 45 | + min(OMD_INSERT_DATETIME) AS OMD_INSERT_DATETIME, |
| 46 | + min(OMD_RECORD_SOURCE_ID) AS OMD_RECORD_SOURCE_ID |
| 47 | + FROM STG.[{{sourceDataObjects.0.name}}] |
| 48 | + GROUP BY {{#each businessKeys}}{{#if @first}}{{#each businessKeyComponentMapping}}{{stringcompare sourceDataItems.0.isHardCodedValue 'True'}}{{#unless @first}},{{/unless}} [{{sourceDataItems.0.name}}]{{/stringcompare}}{{/each}}{{/if}}{{/each}} |
| 49 | + ) LINK_selection |
| 50 | +WHERE CONVERT(CHAR,HASHBYTES('SHA1',UPPER( {{#each businessKeys}} {{#unless @first}}{{!-- Create the Surrogate Key using the combined Business Keys --}} |
| 51 | +{{#each dataItemMappings}} RTRIM(ISNULL([{{targetDataItem.name}}], 'NA')) + '|'+{{/each}} |
| 52 | +{{#each businessKeyComponentMapping}} RTRIM(ISNULL([{{targetDataItem.name}}{{@../index }}], 'NA')) {{#unless @last}} + '|' +{{/unless}}{{/each}}{{#unless @last}} +'|'+{{/unless}}{{/unless}}{{/each}} |
| 53 | +{{#each dataItemMappings}} + RTRIM(ISNULL([{{targetDataItem.name}}], 'NA')) + '|'+{{/each}}{{#each businessKeys}} {{#if @first}})),2) {{/if}}{{/each}} NOT IN |
| 54 | + ( |
| 55 | +SELECT [{{targetDataObject.name}}_SK] |
| 56 | +FROM [INT].[{{targetDataObject.name}}] |
| 57 | +); |
| 58 | +SELECT @@ROWCOUNT AS ROWS_INSERTED |
| 59 | +GO |
| 60 | + {{/each}} |
0 commit comments