1. 程式人生 > >分頁存儲過程(代碼)

分頁存儲過程(代碼)

number bit exec ber into create balloons variable 多少

use BalloonShop
select * from Product


--20個字,1, 5
CREATE PROCEDURE GetProductsOnCatalogPromotion
(@DescriptionLength INT,--描述信息長度
@PageNumber INT, -- 第幾頁
@ProductsPerPage INT, --每頁顯示幾個商品
@HowManyProducts INT OUTPUT -- 一共有多少商品
)
AS
-- declare a new TABLE variable
DECLARE @Products TABLE --表變量
(RowNumber INT
, --在products原始表上增加一個可靠的編號字段 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), ProductID, Name, SUBSTRING(Description, 1, @DescriptionLength) + ... AS Description, Price, Image1FileName, Image2FileName, OnDepartmentPromotion, OnCatalogPromotion FROM Product WHERE OnCatalogPromotion = 1 -- 給輸出參數@HowManyProducts賦值
SELECT @HowManyProducts = COUNT(ProductID) FROM @Products -- extract the requested page of products -- 把請求的第幾頁的內容從@Products表變量中查詢出來 SELECT ProductID, Name, Description, Price, Image1FileName, Image2FileName, OnDepartmentPromotion, OnCatalogPromotion FROM @Products WHERE RowNumber > (@PageNumber - 1) * @ProductsPerPage AND RowNumber <= @PageNumber * @ProductsPerPage GO use BalloonShop --理解表變量 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) INSERT INTO @Products SELECT Row_number() OVER (ORDER BY ProductID) , * from Product where OnCatalogPromotion = 1 select * from @Products go use BalloonShop declare @HowManyProducts int exec GetProductsOnCatalogPromotion 15,2,6, @HowManyProducts out select @HowManyProducts --一共多少頁:總商品數/每頁的產品數 (小數) = 2.1 -- ceiling

分頁存儲過程(代碼)