I am using row number to get paged results back from a stored procedure.
I am finding that ordering using a dynamic case statement column name is slowing things down - but if I hardcode the order by everything is ok.
Is there a way to speed up the dynamic order by without making the WHOLE sql query one string and using SP_EXECUTESQL
ROW_NUMBER() OVER (ORDER BY
CASE WHEN @OrderByColumnName = 'IdentityValue' AND @OrderAscending = 0 THEN CLH.IdentityValue END DESC,
CASE WHEN @OrderByColumnName = 'IdentityValue' AND @OrderAscending = 1 THEN CLH.IdentityValue END
--CLH.CustomerName
) AS [ROW]