declare @ownerId int = ; declare @ownerType varchar(3) = ; select attrId from tbl_PRD_Attributes with (nolock) where ownerId = @ownerId and fsl_tablecode = @ownerType declare @ownerId int = ; declare @ownerType varchar(3) = ; with data as ( SELECT a.attrID as attribute_id, a.attrDescription as attribute, g.attrDescription as category_name FROM tbl_PRD_Attributes a with (nolock) inner join tbl_PRD_AttributeLinks link with (nolock) on link.child_attrTypeID = a.attrTypeID and link.child_attrID = a.attrid AND link.mstr_attrTypeID = 3 and link.mstr_attrId in ( ) INNER join tbl_PRD_Attributes g with (nolock) ON g.attrId = link.mstr_attrId and g.attrDescription = WHERE a.OwnerID = @ownerId AND a.FSL_TableCode = @ownerType AND a.attrTypeID = 4 ) , pagedData as ( select * from data order by category_name, attribute offset #attributes.paging.startRow-1# rows fetch next #attributes.paging.pageSize#10000000 rows only ) select * from pagedData with (nolock) order BY category_name, attribute asc for json auto, include_null_values