declare @ownerId int = ; declare @ownerType varchar(3) = ; declare @objectType varchar(101) = ;; declare @objectId int = ;; with data as ( select uds.objectType, uds.objectId, uds.linkId, uds.linkType, g.groupName as [text] from tbl_SEC_ObjectSharing 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.objectType = @objectType and uds.objectId = @objectId and uds.linkType = 'GRP' union all select uds.objectType, uds.objectId, uds.linkId, uds.linkType, m.firstName + ' ' + m.lastName as [text] from tbl_SEC_ObjectSharing 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.objectType = @objectType and uds.objectId = @objectId and uds.linkType = 'USR' ) #forJSON( 'data', attributes.dataStore, 'linkType desc, text' )#