declare @ownerId int = ; declare @ownerType varchar(3) = ; declare @campaignAttrTypeId int = ; declare @workflowFormAttrTypeId int = ; declare @campaignId int = ; with data as ( select wf.formId, wf.formName, @campaignId linkId, 'GTM' linkType, cast( wf.formId as varchar ) + ',GTM,' + cast( @campaignId as varchar ) as rawToken, cast( case when 1 = 2 or json_value( wf.contactFieldMapping, '$.#fieldName#' ) is not null then 1 else 0 end as bit ) as parseEntity from tbl_WRK_Forms wf with (nolock) inner join tbl_ORG_AttributeLinks oal with (nolock) on oal.child_attrId = wf.formId and oal.child_attrTypeId = @workflowFormAttrTypeId and oal.mstr_attrTypeId = @campaignAttrTypeId where wf.ownerId = @ownerId and wf.ownerType = @ownerType and oal.mstr_attrId = @campaignId ) #forJSON( 'data', attributes.datastore, 'formName' )#