title: Stored procedure - A constant expression was encountered in the ORDER BY list, position 2
body: <p>I have written a stored procedure below in MSSQL. It's throwing error in the ORDER BY clause.</p>
  
  <p>a) It works fine if I put only one condition in ORDER BY clause.</p>
  
  <p>b) I am not sure why it is executing both conditions of the CASE statement in the ORDER BY clause while executing the procedure. Ideally if the first condition is met, it should skip the second.</p>
  
  <p>The error message is:</p>
  
  <blockquote>
    <p>Error: Msg 408, Level 16, State 1, Line 3
    A constant expression was encountered in the ORDER BY list, position 2.</p>
  </blockquote>
  
  <p>This is the query:</p>
  
  <pre><code> SELECT *
   FROM tb_officemaster
   WHERE 1 = 1
       AND officecity LIKE '%gurgaon%'
       AND minprice &gt; 100
       AND maxprice &lt; 1000
   ORDER BY
       CASE WHEN 'priceasc'='priceasc' THEN "minprice" END ASC,
       CASE WHEN 'pricedesc'='priceasc' THEN minprice END DESC 
  </code></pre>