declare @ownerId int = ; declare @ownertype varchar(3) = ; with data as ( select e.eventId, s.surveyName, sq.sortOrder, sq.surveyId, q.questionId, q.questionPrompt from tbl_SVY_Questions q with (nolock) inner join tbl_SVY_SurveyQuestions sq with (nolock) on sq.ownerId = q.ownerId and sq.ownerType = q.ownerType and sq.questionId = q.questionId inner join tbl_Svy_surveys s with (nolock) on s.surveyId = sq.surveyId inner join tbl_EVT_Events e with (nolock) on e.surveyId = s.surveyId where q.ownerid = @ownerId and q.ownerType = @ownerType and q.questionName = 'EVENTQUESTION' and #applyFilter( attributes.datastore.filter )# ) #forJSON( 'data', attributes.datastore, _defaultSortField )#