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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| CREATE PROCEDURE CatalogGetProductsOnDeptPromo
@DepartmentID INT,
@DescriptionLength INT,
@PageNumber INT,
@ProductsPerPage INT,
@HowManyProducts INT OUTPUT
AS
BEGIN
-- 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 P.ProductID
, P.Name,
CASE
WHEN LEN(P.Description) <= @DescriptionLength THEN Product.Description
ELSE SUBSTRING(P.Description, 1, @DescriptionLength) + '...'
END AS Description
, Price
, Thumbnail
, Image
, PromoFront
, PromoDept
FROM dbo.Product AS P
INNER JOIN dbo.ProductCategory AS PC
ON P.ProductID = PC.ProductID
INNER JOIN dbo.Category AS C
ON PC.CategoryID = C.CategoryID
WHERE P.PromoDept = 1
AND C.DepartmentID = @DepartmentID
) AS TMP
END |
Partager