select cdrb.cdr_dstCompanytype, cdrb.cdr_recordId, cdrb.fsl_dstId, cdrp.cdr_dstCompanyType parent_cdr_dstCompanyType, cdrp.cdr_recordId parent_cdr_recordId, cdrp.fsl_dstId parent_fsl_dstId from tbl_CDR_Distributors cdrb with (nolock) left outer join tbl_DST_Distributors dstb with (nolock) on dstb.dstId = cdrb.fsl_dstId left outer join tbl_DST_Distributors dstp with (nolock) on dstp.dstId = dstb.dstParentCo left outer join tbl_CDR_Distributors cdrp with (nolock) on cdrp.ownerId = cdrb.ownerId and cdrp.fsl_tablecode = cdrb.fsl_tablecode and cdrp.fsl_dstId = dstp.dstId where cdrb.cdr_recordId = and cdrb.ownerId = and cdrb.fsl_tablecode = select t.name as tableName from sys.tables t where t.name = 'tbl_DW_DistributorSales_#attributes.ownerType##attributes.ownerId#' select 1 salesPresent where exists ( select * from #_distributorSalesTableName# ds with (nolock) where ds.ownerId = and ds.ownerType = and ( ds.parent_cdr_recordId = or ds.parent_oneFsId = ) and ( ds.cdr_recordId = or ds.oneFsId = ) ) and cdr.cdr_recordId = and cdr.ownerId = and cdr.fsl_tablecode = select 1 salesPresent where exists ( select * from tbl_MFR_TPF_Detail#_tpfSuffix# tpf with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = tpf.tpartnerId and cdr.ownerId = tpf.mfr_id and cdr.fsl_tablecode = 'MFR' where tpf.tpartnerType = 'CDR' and tpf.mfr_id = cdr.ownerId and tpf.forecast_status in ( 'A', 'C' ) and cdr.cdr_recordId = and cdr.ownerId = and cdr.fsl_tablecode = ) select b.cdr_recordId from tbl_CDR_Distributors b with (nolock) inner join tbl_DST_Distributors bf with (nolock) on bf.dstId = b.fsl_dstId inner join tbl_CDR_Distributors p with (nolock) on p.fsl_dstId = bf.dstparentco and p.ownerId = b.ownerId and p.fsl_tablecode = b.fsl_tablecode where b.crmActive = 'Y' and b.ownerId = and b.fsl_tablecode = and p.cdr_recordId = select 1 salesPresent where exists ( select * from tbl_MFR_TPF_Detail#_tpfSuffix# tpf with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.cdr_recordId = tpf.tpartnerId and cdr.ownerid = tpf.mfr_id and cdr.fsl_tablecode = 'mfr' where tpf.tpartnerType = 'CDR' and tpf.mfr_id = cdr.ownerId and tpf.forecast_status in ('A', 'C' ) and cdr.cdr_recordId in ( ) and cdr.ownerId = and cdr.fsl_tablecode = ) --->