select top 1 format( dataDate, 'MM/dd/yyyy' ) dataDate from tbl_DW_1FSDistributors#_tableSuffix# with (nolock) where ownerType = and ownerId = select name from sys.tables where name = update d set d.operatorPurchasing = case when exists ( select * from tbl_DW_OperatorAnalyzer#_tableSuffix# oa where oa.ownerId = d.ownerId and oa.ownerType = d.ownerType and oa.dstId = d.dstId ) then 'Y' else 'N' end from tbl_DW_1fsDistributors#_tableSuffix# d where d.ownerId = and d.ownerType = delete from tbl_DW_1FSDistributors#_tableSuffix# where ownerType = and ownerId = insert into tbl_DW_1FSDistributors#_tableSuffix# ( ownerType, ownerId, cdr_recordId, dstId, dstName, parentDstId, parentDstName, dstType, dstAddr1, dstAddr2, dstCity, dstState, dstZip, dataDate ) select cdr.fsl_tablecode ownerType, cdr.ownerId, cdr.cdr_recordId, d.dstId, d.dstName, case when p.dstId is null then d.dstId else p.dstId end parentDstId, case when p.dstId is null then d.dstName else p.dstName end parentDstName, case when p.dstId is null then 'INDEPENDENT' else 'BRANCH' end as dstType, d.dstAddr1, d.dstAddr2, d.dstCity, d.dstState, d.dstZip, { fn now() } from tbl_CDR_Distributors cdr with (nolock) inner join tbl_DST_Distributors d with (nolock) on d.dstId = cdr.fsl_dstId and d.dstCompanyType = 'B' left outer join tbl_DST_Distributors p with (nolock) on p.dstId = d.dstParentCo and p.dstCompanyType = 'P' where cdr.ownerId = and cdr.fsl_tablecode = update dw set dw.territoryId = ( select top 1 ter.territoryId from tbl_CDR_Distributors cdr with (nolock) inner join tbl_TER_Territories ter with (nolock) on ter.territoryId = cdr.cdr_territoryId and ter.ownerId = cdr.ownerId and ter.fsl_tablecode = cdr.fsl_tablecode where cdr.fsl_dstId = dstId and cdr.ownerId = dw.ownerId and cdr.fsl_tablecode = dw.ownerType and cdr.cdr_dstCompanyType = 'B' ) from tbl_DW_1fsDistributors#_tableSuffix# dw update dw set dw.territoryName = ter.name, dw.territoryPath = ter.territoryPath from tbl_DW_1fsDistributors#_tableSuffix# dw left outer join tbl_TER_Territories ter with (nolock) on ter.territoryId = dw.territoryId and ter.ownerId = dw.ownerId and ter.fsl_tablecode = dw.ownerType select name from sys.tables where name = update d set d.operatorPurchasing = case when exists ( select * from tbl_DW_OperatorAnalyzer#_tableSuffix# oa where oa.ownerId = d.ownerId and oa.ownerType = d.ownerType and oa.dstId = d.dstId ) then 'Y' else 'N' end from tbl_DW_1fsDistributors#_tableSuffix# d where d.ownerId = and d.ownerType =