declare @ownerId int = ; declare @ownerType varchar(3) = ; declare @dashboard varchar(101) = ;; declare @fspro_userId int = ;; with data as ( select uds.fspro_userId, uds.dashboard, uds.linkId, uds.linkType, g.groupName as [text] from tbl_STP_UserDashboardSharing uds with (nolock) inner join tbl_SEC_Groups g with (nolock) on g.ownerId = uds.ownerId and g.fsl_tablecode = uds.ownerType and g.groupId = uds.linkId where uds.ownerId = @ownerId and uds.ownerType = @ownerType and uds.dashboard = @dashboard and uds.fspro_userId = @fspro_userId and uds.linkType = 'GRP' union all select uds.fspro_userId, uds.dashboard, uds.linkId, uds.linkType, m.firstName + ' ' + m.lastName as [text] from tbl_STP_UserDashboardSharing uds with (nolock) inner join tbl_fspro_members m with (nolock) on m.ownerId = uds.ownerId and m.fsl_tablecode = uds.ownerType and m.fspro_userId = uds.linkId where uds.ownerId = @ownerId and uds.ownerType = @ownerType and uds.dashboard = @dashboard and uds.fspro_userId = @fspro_userId and uds.linkType = 'USR' ) #forJSON( 'data', attributes.dataStore, 'linkType desc, text' )#