select ter.name, ter.territoryId, 0 cdr_recordId from tbl_TER_Territories ter where name like '%beverage TSM%' and exists ( select * from tbl_SPL_Opportunities spl inner join tbl_OPR_ClientOperators opr on opr.operatorId = spl.partnerId and opr.ownerId = spl.ownerId and opr.fsltablecode = spl.ownerType left outer join tbl_CDR_Distributors cdr on cdr.cdr_recordId = spl.cdrDistributorId and cdr.ownerId = spl.ownerId and cdr.fsl_tablecode = spl.ownerType where spl.partnerType = 'OPR' and spl.ownerId = ter.ownerId and spl.ownerType = ter.fsl_tablecode and opr.territoryId = ter.territoryId and cdr.cdr_recordId is null ) and ter.ownerId = 26 and ter.name = select cdr_recordId from tbl_CDR_Distributors where cdr_dstName = and cdr_territoryId = and ownerId = 26 and fsl_tablecode = 'MFR' insert into tbl_CDR_Distributors ( ownerId, fsl_tablecode, cdr_dstname, cdr_territoryId ) values ( 26, 'MFR', , ) select spl.opportunityId, spl.cdrDistributorId from tbl_SPL_Opportunities spl inner join tbl_OPR_ClientOperators opr on opr.operatorId = spl.partnerId and opr.ownerId = spl.ownerId and opr.fsltablecode = spl.ownerType left outer join tbl_CDR_Distributors cdr on cdr.cdr_recordId = spl.cdrDistributorId and cdr.ownerId = spl.ownerId and cdr.fsl_tablecode = spl.ownerType where spl.partnerType = 'OPR' and spl.ownerId = 26 and spl.ownerType = 'MFR' and opr.territoryId = and cdr.cdr_recordId is null update tbl_SPL_Opportunities set cdrDistributorId = where opportunityId = select businessPlanId, ownerId, ownerType from tbl_TER_BusinessPlans where ownerId = 26 and ownerType = 'MFR' and timeframePeriod = 'Y01' and territoryId = select ter.name, ter.territoryId, ter.primaryManager from tbl_TER_Territories ter inner join tbl_FSPRO_Members m on m.fspro_userid = ter.primaryManager and m.ownerId = ter.ownerId where name like '%beverage TSM%' and ter.ownerId = 26 and ter.name = update spl set spl.salesPersonUserId = from tbl_SPL_Opportunities spl inner join tbl_OPR_ClientOperators opr on opr.operatorId = spl.partnerId and opr.fsltablecode = spl.ownerType left outer join tbl_FSPRO_Members m on m.fspro_userid = spl.salesPersonUserId and m.ownerId > 0 where m.fspro_userid is null and spl.partnertype = 'OPR' and opr.territoryId = and spl.ownerId = 26 and spl.ownerType = 'MFR'