ALTER PROCEDURE [dbo].[sp_Products_SelectSkipAndTake]
(
@start int,
@end int,
@sortByExpression varchar (200)
)
AS
BEGIN
SET NOCOUNT ON;
SELECT
[ProductID],
[ProductName],
[SupplierID],
[CategoryID],
[QuantityPerUnit],
[UnitPrice],
[UnitsInStock],
[UnitsOnOrder],
[ReorderLevel],
[Discontinued]
FROM [dbo].[Products]
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 @start ROWS
FETCH NEXT @end ROWS ONLY
END