title: CTE Select Order By Dynamically column name
body: <pre><code>;with cte as select * from customer
  </code></pre>
  
  <p>the select cte order by passing parameter</p>
  
  <pre><code>if(@orderby=1)
    begin
      select * from cte order by name
    end
   else if (@oderby=2)
    begin
       select * from cte order by applydate   
    end
   else
      begin
       select * from cte order by customeramount
      end
  </code></pre>
  
  <p>this following error invalid object cte How to Solve this sql query</p>
  
  <p>I have to Solve The Problem </p>
  
  <p>The code is </p>
  
  <pre><code> SELECT * FROM cte ORDER BY CASE @OrderBy WHEN 0 THEN Name ELSE null END , CASE  @OrderBy WHEN 1 THEN  Code ELSE null END ,CASE @OrderBy  WHEN 2 THEN  ApplyAmountTotal ELSE null END ,CASE @OrderBy WHEN 3 THEN ApplyDate ELSE null END 
  </code></pre>