declare @ownerId int = ; declare @ownerType varchar(3) = ; declare @containerAttrTypeId int = #_containerAttrTypeId#; declare @documentAttrTypeId int = #_documentAttrTypeId#; declare @containerId int = #_containerId#; declare @contentPrefix varchar(128) = ; with linkedDocuments as ( select docId, docTitle, docDescription,fileExt, lastUpdatedDate, uploadDate, contentId from tbl_DOC_Documents d with (nolock) where d.ownerId = @ownerId and d.fsl_tablecode = @ownerType and d.docId in ( select mstr_attrId from tbl_ORG_AttributeLinks oal with (nolock) where oal.mstr_attrTypeId = @documentAttrTypeId and oal.child_attrTypeId = @containerAttrTypeId and oal.child_attrId = @containerId ) ) , linkedDocumentsWithContentURL as ( select ra.*, @contentPrefix + c.mainFolder + '/' + c.subFolder + case when nullif( c.subFolder2, '' ) is not null then '/' + c.subFolder2 else '/' end + c.fileName as contentURL from linkedDocuments ra inner join tbl_FSC_Content c with (nolock) on c.contentId = ra.contentId and c.contentId != 755 ) #forJSON( 'linkedDocumentsWithContentURL', attributes.dataStore, 'docTitle' )#