1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| procedure stockee
CREATE PROCEDURE CatalogGetProductsOnDeptPromo
(@DepartmentID INT,
@DescriptionLength INT,
@PageNumber INT,
@ProductsPerPage INT,
@HowManyProducts INT OUTPUT)
AS
-- declare a new TABLE variable
DECLARE @Products TABLE
(RowNumber INT,
ProductID INT,
Name NVARCHAR(50),
Description NVARCHAR(MAX),
Price MONEY,
Thumbnail NVARCHAR(50),
Image NVARCHAR(50),
PromoFront bit,
PromoDept bit)
-- populate the table variable with the complete list of products
INSERT INTO @Products
SELECT ROW NUMBER() OVER (ORDER BY ProductID) AS Row,
ProductID, Name, SUBSTRING(Description, 1, @DescriptionLength)
+ '...' AS Description,
Price, Thumbnail, Image, PromoFront, PromoDept
FROM
(SELECT DISTINCT Product.ProductID, Product.Name,
CASE WHEN LEN(Product.Description) <= @DescriptionLength
THEN Product.Description
ELSE SUBSTRING(Product.Description, 1, @DescriptionLength) + '...' END
AS Description, Price, Thumbnail, Image, PromoFront, PromoDept
FROM Product INNER JOIN ProductCategory
ON Product.ProductID = ProductCategory.ProductID
INNER JOIN Category
ON ProductCategory.CategoryID = Category.CategoryID
WHERE Product.PromoDept = 1
AND Category.DepartmentID = @DepartmentID |
Partager