select partnerType from tbl_CRM_Interactions with (nolock) where ownerId = and ownerType = and interactionId = declare @ownerId int = ; declare @ownerType varchar(3) = ; declare @interactionId int = ; with data as ( select i.interactionId, i.partnerId, i.partnerType, i.contactId, opr.companyName as partnerName, opr.oprType partnerPriority, ter.name as territoryName, i.disposition, i.interactionDate, m.firstName + ' ' + m.lastName as salesRepFullName, opr.fsl_choId as oneFSId, opr.oprCompanyType as companyType, opr.phone, opr.address, opr.city, opr.state, opr.zipCode, i.cdrDistributorId from tbl_CRM_Interactions i with (nolock) inner join tbl_OPR_ClientOperators opr with (nolock) on opr.ownerId = i.ownerId and opr.fsltablecode = i.ownerType and opr.operatorId = i.partnerId inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = opr.ownerId and ter.fsl_tablecode = opr.fsltablecode and ter.territoryId = opr.territoryId left outer join tbl_FSPRO_Members m with (nolock) on m.fspro_userId = i.dispositionUserId and m.ownerId > 0 where i.interactionId = @interactionId and i.ownerId = @ownerId and i.ownerType = @ownerType select i.interactionId, i.partnerId, i.partnerType, i.contactId, cdr.cdr_dstName as partnerName, cast( null as varchar) partnerPriority, ter.name as territoryName, i.disposition, i.interactionDate, m.firstName + ' ' + m.lastName as salesRepFullName, cdr.fsl_dstId as oneFSId, cdr.cdr_dstCompanyType companyType, cdr.cdr_dstPhone as phone, cdr.cdr_dstAddress1 as address, cdr.cdr_dstCity as city, cdr.cdr_dstState as state, cdr.cdr_dstZip as zipCode, null as cdrDistributorId from tbl_CRM_Interactions i with (nolock) inner join tbl_CDR_Distributors cdr with (nolock) on cdr.ownerId = i.ownerId and cdr.fsl_tablecode = i.ownerType and cdr.cdr_recordId = i.partnerId inner join tbl_TER_Territories ter with (nolock) on ter.ownerId = cdr.ownerId and ter.fsl_tablecode = cdr.fsl_tablecode and ter.territoryId = cdr.cdr_territoryId inner join tbl_FSPRO_Members m with (nolock) on m.fspro_userId = i.dispositionUserId and m.ownerId > 0 where i.interactionId = @interactionId and i.ownerId = @ownerId and i.ownerType = @ownerType ) #forJSON( 'data', attributes.datastore, "interactionId" )#