• 利用存储过程进行分页


    1.大数据通用高效分页

    create PROCEDURE [dbo].[GetPageDataOut]
    (
    @tn nvarchar(30),--表名称
    @idn nvarchar(20),--表主键名称
    @pi int = 1,--当前页数
    @ps int = 7,--每页大小
    @rc int output,--总行数(传出参数)
    @pc int output--总页数(传出参数)
    )
    AS
    DECLARE @sql NVARCHAR(225),@sqlCount NVARCHAR(225)
    SET @sqlCount = 'SELECT @rc=COUNT(['+@idn+']),@pc=CEILING((COUNT('+@idn+')+0.0)/'+ CAST(@ps AS VARCHAR)+') FROM ' + @tn
    EXEC SP_EXECUTESQL @sqlCount,N'@rc INT OUTPUT,@pc INT OUTPUT',@rc OUTPUT,@pc OUTPUT

    IF @pi = 1
    BEGIN
    SET @sql='SELECT TOP '+str(@ps) +' * FROM '+@tn
    EXEC(@sql)
    END
    ELSE
    BEGIN
    SET NOCOUNT ON
    DECLARE @PageLowerBound INT,@StartID INT
    SET @PageLowerBound = @ps * (@pi-1)
    IF @PageLowerBound<1
    SET @PageLowerBound=1
    SET ROWCOUNT @PageLowerBound
    SET @sql=N'SELECT @StartID = ['+@idn+'] FROM '+@tn
    print '@sql='+@sql
    EXEC sp_executesql @sql,N'@StartID int output',@StartID output
    SET ROWCOUNT 0
    SET @sql='SELECT TOP '+str(@ps) +' * FROM '+@tn+' WHERE ['+@idn+']>'+ str(@StartID)
    EXEC(@sql)
    SET NOCOUNT OFF
    END

    --测试语句
    declare @rc int,@pc int
    exec GetPageDataOut 'Ams_Area','ar_id',2,15,@rc output,@pc output
    select @rc,@pc


    2.简单通用分页

    (
    @tn nvarchar(30),--表名称
    @idn nvarchar(20),--表主键名称
    @pi int = 1,--当前页数
    @ps int = 7,--每页大小
    @wh nvarchar(255) = '',--wehre查询条件
    @oby nvarchar(255) = '',--orderby 排序
    @rc int output,--总行数(传出参数)
    @pc int output--总页数(传出参数)
    )
    AS
    DECLARE @sql NVARCHAR(225)='',@sqlCount NVARCHAR(225)=''
    --1.计算总行数和总页数
    SET @sqlCount = 'SELECT @rc=COUNT(['+@idn+']),@pc=CEILING((COUNT('+@idn+')+0.0)/'+ CAST(@ps AS VARCHAR)+') FROM ' + @tn
    IF LEN(@wh)>1
    set @sqlCount=@sqlCount+' WHERE '+@wh
    print @sqlCount
    EXEC SP_EXECUTESQL @sqlCount,N'@rc INT OUTPUT,@pc INT OUTPUT',@rc OUTPUT,@pc OUTPUT
    --2.分页
    --2.1如果是第一页,则直接查询
    IF @pi = 1
    BEGIN
    SET @sql='SELECT TOP '+str(@ps) +' * FROM '+@tn
    IF LEN(@wh)>1
    set @sql=@sql+' WHERE '+@wh
    IF LEN(@oby)>1
    SET @sql=@sql+' order by ' +@oby
    EXEC(@sql)
    END
    ELSE--2.2如果不是第一页,则拼接查询语句
    BEGIN
    SET NOCOUNT ON
    SET @sql='SELECT * FROM (select row_number() over(order by '
    IF LEN(@oby)>1
    set @sql=@sql + @oby+') as rowNum,* from '+@tn
    else
    set @sql=@sql + @idn+') as rowNum,* from '+@tn
    IF LEN(@wh)>1
    set @sql=@sql+' where '+@wh
    set @sql=@sql+')as temp where rowNum>'+str(@ps * (@pi-1))+' and rowNum<='+str(@ps*@pi)
    print @sql
    EXEC(@sql)
    SET NOCOUNT OFF
    END

    --测试语句
    declare @rc int,@pc int
    exec [GetPageDataOutRowNumber] 'Ams_Area','ar_id',2,5,'',' ar_id desc',@rc output,@pc output
    select @rc,@pc

  • 相关阅读:
    Dojo(发音:豆粥)的目标是解决DHTML应用程序长期存在的历史问题
    如何用Fireworks制作图标的透明效果
    2021.09.15膜你赛
    2021.09.14 膜你赛
    2021.08.29 膜你赛
    2021.08.28 膜你赛
    2021.10.06 膜你赛
    2021.09.18 膜你赛
    2021.09.13膜你赛
    2021.10.05膜你赛
  • 原文地址:https://www.cnblogs.com/a178172698/p/3292504.html
Copyright © 2020-2023  润新知