CREATE PROCEDURE [dbo].[Products_SelectSkipAndTakeByCategoryID] ( @categoryID int, @start int, @numberOfRows int, @sortByExpression varchar(200) ) AS BEGIN SET NOCOUNT ON; DECLARE @numberOfRowsToSkip int = @start; SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued] FROM [dbo].[Products] WHERE ([CategoryID] = @categoryID AND 1 = CASE WHEN @categoryID IS NULL THEN 0 ELSE 1 END) OR ([CategoryID] IS NULL AND 1 = CASE WHEN @categoryID IS NULL THEN 1 ELSE 0 END) ORDER BY CASE WHEN @sortByExpression = 'ProductID' THEN [ProductID] END, CASE WHEN @sortByExpression = 'ProductID desc' THEN [ProductID] END DESC, CASE WHEN @sortByExpression = 'ProductName' THEN [ProductName] END, CASE WHEN @sortByExpression = 'ProductName desc' THEN [ProductName] END DESC, CASE WHEN @sortByExpression = 'SupplierID' THEN [SupplierID] END, CASE WHEN @sortByExpression = 'SupplierID desc' THEN [SupplierID] END DESC, CASE WHEN @sortByExpression = 'CategoryID' THEN [CategoryID] END, CASE WHEN @sortByExpression = 'CategoryID desc' THEN [CategoryID] END DESC, CASE WHEN @sortByExpression = 'QuantityPerUnit' THEN [QuantityPerUnit] END, CASE WHEN @sortByExpression = 'QuantityPerUnit desc' THEN [QuantityPerUnit] END DESC, CASE WHEN @sortByExpression = 'UnitPrice' THEN [UnitPrice] END, CASE WHEN @sortByExpression = 'UnitPrice desc' THEN [UnitPrice] END DESC, CASE WHEN @sortByExpression = 'UnitsInStock' THEN [UnitsInStock] END, CASE WHEN @sortByExpression = 'UnitsInStock desc' THEN [UnitsInStock] END DESC, CASE WHEN @sortByExpression = 'UnitsOnOrder' THEN [UnitsOnOrder] END, CASE WHEN @sortByExpression = 'UnitsOnOrder desc' THEN [UnitsOnOrder] END DESC, CASE WHEN @sortByExpression = 'ReorderLevel' THEN [ReorderLevel] END, CASE WHEN @sortByExpression = 'ReorderLevel desc' THEN [ReorderLevel] END DESC, CASE WHEN @sortByExpression = 'Discontinued' THEN [Discontinued] END, CASE WHEN @sortByExpression = 'Discontinued desc' THEN [Discontinued] END DESC OFFSET @numberOfRowsToSkip ROWS FETCH NEXT @numberOfRows ROWS ONLY END