declare @ownerId int = ; declare @ownerType varchar(3) = ; declare @campaignAttrTypeId int = ; declare @territoryAttrTypeId int = ; declare @campaignId int = ; with data as ( select ter.territoryId, ter.territoryPath, ter.name as territoryName from tbl_TER_territories ter with (nolock) inner join tbl_ORG_AttributeLinks oal with (nolock) on oal.child_attrId = ter.territoryId and oal.child_attrTypeId = @territoryAttrTypeId and oal.mstr_attrTypeId = @campaignAttrTypeId where ter.ownerId = @ownerId and ter.fsl_tablecode = @ownerType and oal.mstr_attrId = @campaignId ) #forJSON( 'data', attributes.datastore, 'territoryPath' )#