title: Stored procedure to get paged sorted results with a computed data column
body: <p>I want to make a stored procedure to get paged sorted result but here's the twist. I have to add a column and fill it with computed/calculated data. Currently I have </p>
  
  <pre><code>ALTER proc [dbo].[pr_GetPagedSortedOrdersByUser]      
  (
   @userid numeric(18,0),              
   @CurrentPage int,              
   @PageSize int,            
   @SortField varchar(200),            
   @SortOrder varchar(5),        
   @status varchar(50)        
  )              
  as              
  begin              
  with temp as              
    (              
     select * 
      ,ROW_NUMBER() OVER (order by            
          CASE WHEN @SortField = 'orderdate' AND @SortOrder = 'Desc' THEN orderdate END DESC,            
          CASE WHEN @SortField = 'orderdate' THEN orderdate END,      
          CASE WHEN @SortField = 'netamount' AND @SortOrder = 'Desc' THEN netamount END DESC,            
          CASE WHEN @SortField = 'netamount' THEN netamount END,      
          CASE WHEN @SortField = 'id' AND @SortOrder = 'Desc' THEN id END DESC,            
          CASE WHEN @SortField = 'id' THEN id END                       
         ) AS ROWID              
     from tbOrder    
     where (@status='All' or delivered=@status)and userid=@userid
  
    )              
    select * ,
    (
      netamount - discount
    ) as 'Gross'     
     from temp             
    where rowid&gt;((@CurrentPage - 1) * @PageSize) and rowid &lt;(@CurrentPage * @PageSize + 1)              
  
    select count(*) as [count]            
     from tbOrder    
     where (delivered=@status or @status='All')and userid=@userid       
  end
  </code></pre>
  
  <p><strong>NOTE:</strong> <code>Gross</code> column is not present in database and I should be able to pass value <code>Gross</code> to <code>@SortField</code></p>