2011-08-11 34 views
-2

我是SQL Server的新手,目前正在学习它。我有下面的存储过程,我不明白。将存储过程转换为简单的T-Sql

-- declare a new TABLE variable 
DECLARE @Products TABLE 
(RowNumber INT, 
ProductID INT, 
Name VARCHAR(50), 
Description VARCHAR(5000), 
Price MONEY, 
Image1FileName VARCHAR(50), 
Image2FileName VARCHAR(50), 
OnDepartmentPromotion bit, 
OnCatalogPromotion bit) 

-- populate the table variable with the complete list of products 
INSERT INTO @Products 
SELECT ROW_NUMBER() OVER (ORDER BY Product.ProductID), 
Product.ProductID, Name, 
SUBSTRING(Description, 1, @DescriptionLength) + '...' AS Description, Price, 
Image1FileName, Image2FileName, OnDepartmentPromotion, OnCatalogPromotion 
FROM Product INNER JOIN ProductCategory 
ON Product.ProductID = ProductCategory.ProductID 
WHERE ProductCategory.CategoryID = @CategoryID 
-- return the total number of products using an OUTPUT variable 
SELECT @HowManyProducts = COUNT(ProductID) FROM @Products 
-- extract the requested page of products 
SELECT ProductID, Name, Description, Price, Image1FileName, 
Image2FileName, OnDepartmentPromotion, OnCatalogPromotion 
FROM @Products 
WHERE RowNumber > (@PageNumber - 1) * @ProductsPerPage 
AND RowNumber <= @PageNumber * @ProductsPerPage 

请!将上面的存储过程转换为简单的t-sql语句,以便我获得这些点。我会非常感谢你的工作。

在此先感谢

+4

是这个家庭作业?如果是这样,那么你应该把它标记为家庭作业。你也应该展示你曾经尝试过的。 – Taryn

+0

积分?什么意思? –

+1

这里没有人会为你做你的工作。如果您需要某个特定问题的帮助,请更改您的问题,并告诉我们您已为此问题付出了一些努力。否则,你根本不可能得到任何帮助。 –

回答

1

像这样的工作(我没有测试):

--extract the requested page of products 
SELECT ProductID, Name, Description, Price, Image1FileName, 
Image2FileName, OnDepartmentPromotion, OnCatalogPromotion 
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY Product.ProductID) AS RowNumber, 
Product.ProductID, Name, 
SUBSTRING(Description, 1, @DescriptionLength) + '...' AS Description, Price, 
Image1FileName, Image2FileName, OnDepartmentPromotion, OnCatalogPromotion 
FROM Product INNER JOIN ProductCategory 
ON Product.ProductID = ProductCategory.ProductID 
WHERE ProductCategory.CategoryID = @CategoryID 
    ) A 
WHERE RowNumber > (@PageNumber - 1) * @ProductsPerPage 
AND RowNumber <= @PageNumber * @ProductsPerPage 

-- return the total number of products using an OUTPUT variable 
SELECT COUNT(ProductID) AS ProductCount FROM Product INNER JOIN ProductCategory 
ON Product.ProductID = ProductCategory.ProductID 
WHERE ProductCategory.CategoryID = @CategoryID