This SQL statement is being flagged in SQL Server in the ORDER BY ' + @SortField + ' ' + @SortOrder + ' part.. Any ideas how can I fix it?
ALTER PROC [sbuser].[sp_MemberMailList ]
@MemberMailID bigint = null,
@FromMemberID bigint = null,
@ToMemberID bigint = null,
@Subject varchar(150) = null,
@Message varchar(8000) = null,
@FromDeletedFlag bit = null,
@ToDeletedFlag bit = null,
@FromArchivedFlag bit = null,
@ToArchivedFlag bit = null,
@ReadFlag bit = null,
@SQL nvarchar(4000) = null,
@SortField varchar(100) = null,
@SortOrder varchar(25) = null,
@NotificationSent bit = null,
@MemberID bigint = null,
@OnHold bit = 0,
@SpecialMail varchar(1) = 'N',
@PageSize float = null,
@PageNum int = 1,
@TotalPages float = null,
@StartDate datetime = null,
@EndDate datetime = null,
@MODE varchar(50)
AS
IF @MODE = 'INBOX-MAIL'
BEGIN
SELECT @TotalPages = COUNT(*)/@PageSize
FROM MemberMail a
INNER JOIN Member b ON b.MemberID = a.FromMemberID
WHERE a.ToMemberID = @ToMemberID
AND a.ToDeletedFlag = 0
AND a.OnHold = 0
AND a.ToArchivedFlag = 0;
WITH InMails AS
(
SELECT ROW_NUMBER() OVER(ORDER BY ' + @SortField + ' ' + @SortOrder + ') AS RowNum,
a.MemberMailID,
a.FromMemberID,
a.Subject,
a.CreateDate,
b.UserName,
a.ToReadFlag,
b.Firstname,
b.Lastname,
b.MemberDisplayName AS DisplayName
FROM MemberMail a
INNER JOIN Member b ON b.MemberID = a.FromMemberID
WHERE a.ToMemberID = @ToMemberID
AND a.ToDeletedFlag = 0
AND a.OnHold = 0
AND a.ToArchivedFlag = 0
)
SELECT *
FROM InMails
WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize
ORDER BY CreateDate DESC
END
Any help would be appreciated..
Many thanks