有10家公司,每家公司有10种产品。也就是总共有100中产品。现在需要分页查询并显示出来,每页显示10条数据,产品类型和所属公司都不能重复,请给出解决方案和详细sql语句。

解决方案 »

  1.   

    /*分页查找数据*/ 
      CREATE PROCEDURE [dbo].[GetRecordSet] 
      @strSql varchar(8000),--查询sql,如select * from [user] 
      @PageIndex int,--查询当页号 
      @PageSize int--每页显示记录 
      AS 
      set nocount on 
      declare @p1 int 
      declare @currentPage int 
      set @currentPage = 0 
      declare @RowCount int 
      set @RowCount = 0 
      declare @PageCount int 
      set @PageCount = 0 
      exec sp_cursoropen @p1 output,@strSql,@scrollopt=1,@ccopt=1,@rowcount=@rowCount output --得到总记录数 
      select @PageCount=ceiling(1.0*@rowCount/@pagesize) --得到总页数 
      ,@currentPage=(@PageIndex-1)*@PageSize+1 
      select @RowCount,@PageCount 
      exec sp_cursorfetch @p1,16,@currentPage,@PageSize 
      exec sp_cursorclose @p1 
      set nocount off 
      GO本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/10/28/4740536.aspx
      

  2.   

    --要分页的原始数据
    CREATE TABLE tb(
    ID    int PRIMARY KEY,  --记录编号
    grade  varchar(10),     --类别名称
    uptime datetime)        --更新时间
    INSERT tb SELECT 1 ,'a','2004-12-11'
    UNION ALL SELECT 2 ,'b','2004-12-11'
    UNION ALL SELECT 3 ,'c','2004-12-11'
    UNION ALL SELECT 4 ,'a','2004-12-12'
    UNION ALL SELECT 5 ,'c','2004-12-13'
    UNION ALL SELECT 6 ,'c','2004-12-13'
    UNION ALL SELECT 7 ,'a','2004-12-14'
    UNION ALL SELECT 8 ,'a','2004-12-15'
    UNION ALL SELECT 9 ,'b','2004-12-16'
    UNION ALL SELECT 10,'b','2004-12-17'
    UNION ALL SELECT 11,'a','2004-12-17'--分页定义表
    CREATE TABLE tb_Page(
    grade   varchar(10) PRIMARY KEY, --类别名称,与tb表的grade关联
    Records int,                     --每页显示的记录数
    Orders  int)                     --在页中的显示顺序
    INSERT tb_Page SELECT 'c',2,1
    UNION  ALL     SELECT 'b',1,2
    UNION  ALL     SELECT 'a',2,3
    GO--实现分页处理的存储过程
    CREATE PROC p_PageView
    @PageCurrent int=1  --要显示的当前页码
    AS
    SET NOCOUNT ON
    --得到每页的记录数
    DECLARE @PageSize int
    SELECT @PageSize=SUM(Records) FROM tb_Page
    IF ISNULL(@PageSize,0)<0 RETURN--分页显示处理
    SET @PageCurrent=@PageCurrent*@PageSize
    SET ROWCOUNT @PageCurrent
    SELECT SID=IDENTITY(int,1,1),ID 
    INTO # FROM(
    SELECT TOP 100 PERCENT a.ID 
    FROM tb a
    LEFT JOIN tb_Page b ON a.grade=b.grade
    ORDER BY CASE WHEN b.grade IS NULL THEN 1 ELSE 0 END,--分类没有定义的显示在最后
    ((SELECT COUNT(*) FROM tb 
    WHERE grade=a.grade 
    AND (uptime>a.uptime OR uptime=a.uptime AND id>=a.id))-1)
    /b.Records,
    b.Orders,a.ID DESC)a
    IF @PageCurrent>@PageSize
    BEGIN
    SET @PageCurrent=@PageCurrent-@PageSize
    SET ROWCOUNT @PageCurrent
    DELETE FROM #
    END
    SELECT a.* FROM tb a,# b
    WHERE a.ID=b.ID
    ORDER BY b.SID
    GO--调用
    EXEC p_PageView 2
    /*--结果
    ID          grade      uptime                                                 
    ----------- ---------- ------------------------------------------------------ 
    3           c          2004-12-11 00:00:00.000
    9           b          2004-12-16 00:00:00.000
    7           a          2004-12-14 00:00:00.000
    4           a          2004-12-12 00:00:00.000
    2           b          2004-12-11 00:00:00.000
    --*/