zoukankan      html  css  js  c++  java
  • 分页存储过程(代码)

    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 
  • 相关阅读:
    C# 中使用using的三种方法
    RabbitMQ系列文章
    c# 枚举(Enum)
    c# 占位符 {0} {1}
    C#中Lambda表达式总结
    c# 类型转换 int.TryParse() 方法,日期转换
    sql select 0 字段 某字段是不在指定的表,编一个字段
    Apache服务器安装配置(win版)
    安装 Apache 出现 <OS 10013> 以一种访问权限不允许的方式做了一个访问套接字的尝试
    windows安装Apache HTTP服务器报错:无法启动,因为应用程序的并行配置不正确
  • 原文地址:https://www.cnblogs.com/fllowerqq/p/8970594.html
Copyright © 2011-2022 走看看