select dst.dstId, cs.sku, cs.mfrId, cs.customerSKU, cs.customerSKUDesc, cs.customerSoldByUM from tbl_PRD_CustomerSkus cs with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = cs.customerId and cdr.ownerId = cs.ownerId and cdr.fsl_tablecode = cs.fsl_tablecode inner join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId where cs.ownerId = and cs.fsl_tablecode = and cs.linkType = 'SKU' and cs.skuId = and exists ( select * from tbl_CDR_Distributors cdr2 with (nolock) where cdr2.ownerId = and cdr2.fsl_tablecode = and cdr2.fsl_dstId = dst.dstId ) order by dst.dstId, cs.customerSKU select dst.dstId, cs.sku, cs.mfrId, cs.customerSKU, cs.customerSKUDesc, cs.customerSoldByUM from tbl_PRD_CustomerSkus cs with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = cs.customerId and cdr.ownerId = cs.ownerId and cdr.fsl_tablecode = cs.fsl_tablecode inner join tbl_DST_Distributors dst with (nolock) on dst.dstId = cdr.fsl_dstId where cs.ownerId = and cs.fsl_tablecode = and cs.linkType = 'SKU' and cs.skuId = order by dst.dstId, cs.customerSKU select top 1 cdr.cdr_recordId, case when cdr_dstCompanyType = 'P' then 'PDST' else 'DST' end companyType, cdr.cdr_dstName from tbl_CDR_Distributors cdr with (nolock) where ownerId = and fsl_tablecode = and fsl_dstId = order by fsl_mapPriority insert into tbl_PRD_CustomerSkus( ownerId, fsl_tablecode, sku, skuId, linktype, customerSKU, customerskuDesc, customerSoldByUM, customerType, customerId, mfrId ) values ( , , , , 'SKU', , , , , , ) select top 1 cdr.cdr_recordId, case when cdr_dstCompanyType = 'P' then 'PDST' else 'DST' end companyType, cdr.cdr_dstName from tbl_CDR_Distributors cdr with (nolock) where ownerId = and fsl_tablecode = and fsl_dstId = order by fsl_mapPriority update tbl_PRD_CustomerSKUs set ownerId = ownerId , #_fc# = where ownerId = and fsl_tablecode = and skuId = and linkType = 'SKU' and customerId = and customerType =