Home > Mobile >  Why this query is not working? A constant expression was encountered in the ORDER BY list, position
Why this query is not working? A constant expression was encountered in the ORDER BY list, position

Time:11-23

I try to execute this query, but I get this error from SQL Server:

A constant expression was encountered in the ORDER BY list, position 1

SET DATEFORMAT DMY
SET NOCOUNT ON

DECLARE @TotalRows INT

SELECT
    [Failure].[FailureId] AS [FailureId],
    [Failure].[HTTPCode] AS [HTTPCode],
    [Failure].[EmergencyLevel] AS [EmergencyLevel],
    [Failure].[Message] AS [Message],
    [Failure].[StackTrace] AS [StackTrace],
    [Failure].[Source] AS [Source],
    [Failure].[Comment] AS [Comment],
    [Failure].[Active] AS [Active],
    [Failure].[UserCreationId] AS [UserCreationId],
    [Failure].[UserLastModificationId] AS [UserLastModificationId],
    [Failure].[DateTimeCreation] AS [DateTimeCreation],
    [Failure].[DateTimeLastModification] AS [DateTimeLastModification]
FROM 
    [Failure]
WHERE
    1 = 1
    AND ('' = '' OR ([Failure].[FailureId] LIKE  '%'   ''   '%'))
ORDER BY
    CASE WHEN ('FailureId' = 'FailureId' AND 1 = 0) THEN [FailureId] END ASC,
    CASE WHEN ('FailureId' = 'FailureId' AND 1 = 1) THEN [FailureId] END DESC,
    CASE WHEN ('FailureId' = 'HTTPCode' AND 1 = 0) THEN [HTTPCode] END ASC,
    CASE WHEN ('FailureId' = 'HTTPCode' AND 1 = 1) THEN [HTTPCode] END DESC,
    CASE WHEN ('FailureId' = 'EmergencyLevel' AND 1 = 0) THEN [EmergencyLevel] END ASC,
    CASE WHEN ('FailureId' = 'EmergencyLevel' AND 1 = 1) THEN [EmergencyLevel] END DESC,
    CASE WHEN ('FailureId' = 'Message' AND 1 = 0) THEN [Message] END ASC,
    CASE WHEN ('FailureId' = 'Message' AND 1 = 1) THEN [Message] END DESC,
    CASE WHEN ('FailureId' = 'StackTrace' AND 1 = 0) THEN [StackTrace] END ASC,
    CASE WHEN ('FailureId' = 'StackTrace' AND 1 = 1) THEN [StackTrace] END DESC,
    CASE WHEN ('FailureId' = 'Source' AND 1 = 0) THEN [Source] END ASC,
    CASE WHEN ('FailureId' = 'Source' AND 1 = 1) THEN [Source] END DESC,
    CASE WHEN ('FailureId' = 'Comment' AND 1 = 0) THEN [Comment] END ASC,
    CASE WHEN ('FailureId' = 'Comment' AND 1 = 1) THEN [Comment] END DESC,
    CASE WHEN ('FailureId' = 'Active' AND 1 = 0) THEN [Active] END ASC,
    CASE WHEN ('FailureId' = 'Active' AND 1 = 1) THEN [Active] END DESC,
    CASE WHEN ('FailureId' = 'UserCreationId' AND 1 = 0) THEN [UserCreationId] END ASC,
    CASE WHEN ('FailureId' = 'UserCreationId' AND 1 = 1) THEN [UserCreationId] END DESC,
    CASE WHEN ('FailureId' = 'UserLastModificationId' AND 1 = 0) THEN [UserLastModificationId] END ASC,
    CASE WHEN ('FailureId' = 'UserLastModificationId' AND 1 = 1) THEN [UserLastModificationId] END DESC,
    CASE WHEN ('FailureId' = 'DateTimeCreation' AND 1 = 0) THEN [DateTimeCreation] END ASC,
    CASE WHEN ('FailureId' = 'DateTimeCreation' AND 1 = 1) THEN [DateTimeCreation] END DESC,
    CASE WHEN ('FailureId' = 'DateTimeLastModification' AND 1 = 0) THEN [DateTimeLastModification] END ASC,
    CASE WHEN ('FailureId' = 'DateTimeLastModification' AND 1 = 1) THEN [DateTimeLastModification] END DESC
OFFSET (1 - 1) * 2 ROWS
FETCH NEXT 2 ROWS ONLY

CodePudding user response:

The error text of

"A constant expression was encountered in the ORDER BY list, position 1"

is due to the

"AND 1 = 0)"

condition of the first ORDER BY CASE line. Same for the other CASE lines.

Both sides of an equal sign condition should not be constants.

  • Related