I can't figure out how to rewrite this sql statement to avoid the error below
Msg 8120, Level 16, State 1, Line 26 Column 'StaffMember.StaffMemberId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SQL Statement:
SELECT (
SELECT [dbo].[udf_ReadStaffMemberBasic](SM.StaffMemberId)
FOR XML PATH('StaffMemberSummary'),
ROOT('Items'),
TYPE
),
COUNT(SM.StaffMemberId) AS TotalResults,
CEILING(CAST(COUNT(SM.StaffMemberId) AS DECIMAL) / @PageSize) AS TotalPages
FROM StaffMember SM
INNER JOIN StaffMemberToAdditionalRole AR
ON SM.StaffMemberId = AR.StaffMemberId
WHERE (
SM.Firstname LIKE '%' + @Name + '%'
OR SM.Surname LIKE '%' + @Name + '%'
)
AND (
@FloorId IS NULL
OR SM.FloorId = @FloorId
)
AND (
@DirectorateId IS NULL
OR SM.DirectorateId = @DirectorateId
)
AND (
@AdditionalRoleId IS NULL
OR AR.AdditionalRoleId = @AdditionalRoleId
)
ORDER BY SM.StaffMemberId OFFSET @Page * @PageSize ROWS
FETCH NEXT @PageSize ROWS ONLY
What I'm trying to do is to return a list of Staff members from a function and pagination data all as XML.