Not a Sunday
delete from tbl_EXPORT_Kellanova_Dist_SKU where ownerId = and ownertype = and eai_batchId = drop table if exists [#_customerSKUsTable#]; drop table if exists [#_customerSKUsTable#]; with x as ( select cs.customerId as cdr_recordId, cs.skuId, cs.customerSKU, row_number() over( partition by customerId, skuId order by customerKey ) ranking from tbl_PRD_CustomerSkus cs with (nolock) where cs.ownerId = 207 and cs.fsl_tablecode= 'MFR' and nullif( cs.customerSKU, '' ) is not null ) select * into [#_customerSKUsTable#] from x where ranking = 1; alter table [#_customerSKUsTable#] add constraint [pk_#_customerSKUsTable#] primary key ( cdr_recordId, skuId ); with export as ( select cdr.ownerType, cdr.ownerId, 0 as eai_batchId, row_number() over ( order by ph.sku, cdr.cdr_dstName ) as eai_lineNumber, ph.sku kellanova_sku, cdr.cdr_dstName as distributor_name, cs.customerSKU as distributor_sku, cs.cdr_recordId as distributor_code from [#_customerSKUsTable#] cs with (nolock) inner join tbl_DW_CRMDistributors_MFR207 cdr with (nolock) on cdr.cdr_recordId = cs.cdr_recordId and cdr.ownerId = 207 and cdr.ownerType = 'MFR' inner join tbl_DW_ProductHierarchy_MFR207 ph with (nolock) on ph.skuId = cs.skuId and ph.ownerId = 207 and ph.ownerType = 'MFR' and ph.crmActive = 'Y' and ph.product != 'WK KELLOGG' and ph.discontinued <> 'Y' ) insert into tbl_EXPORT_Kellanova_Dist_SKU ( ownerType, ownerId, eai_batchId, eai_lineNumber, kellanova_sku, distributor_name, distributor_sku, distributor_code ) select ownerType, ownerId, #qmd_batch.batchId# as eai_batchId, eai_lineNumber, kellanova_sku, distributor_name, distributor_sku, distributor_code from export order by eai_lineNumber select * from tbl_EXPORT_Kellanova_Dist_SKU with (nolock) where ownerId = and ownertype = and eai_batchId = order by eai_lineNumber drop table if exists [#_customerSKUsTable#]; update tbl_EAI_InboundBatches set loadedColumns = 'kellanova_sku,distributor_name,distributor_sku,distributor_code' where ownerId = and ownertype = and batchId =
Exported #lsnumberformat( _exportCount )# Customer SKUs