zoukankan      html  css  js  c++  java
  • SQLSERVER分页存储过程

    单分页存储过程

     CREATE PROCEDURE [dbo].[sp_All_PagerList]

        @tblName nvarchar(255),--表名

        @fldNames nvarchar(1000)='*',--字段列表(默认所有字段)

        @fldOrderName nvarchar(255),--主键字段(排序用)

        @orderType bit =0,--排序类型,非0则降序(默认升序)

        @pageIndex int=1,--页码

        @pageSize int =20,--页大小

        @strWhere nvarchar(1500)='',--查询条件,不带where

        @limitCount int =1000000, --限制最多取5000条数据

        @totalCount int output --总记录数

    AS

    BEGIN

        SET NOCOUNT ON;

        declare @strSQL nvarchar(4000)--主语句

        declare @strTable nvarchar(500)--主查询临时表

        declare @strOrderSQL nvarchar(1000)--总数查询语句

        declare @strOutParam nvarchar(500)--输出参数

        declare @strTmp nvarchar(255)--临时分页字句 max 和min

        declare @strOrder nvarchar(255)--排序参数

            

        --设置排序

        if(@orderType != 0)

        begin

            set @strTmp = ' <(select min '

            set @strOrder = ' order by [' + @fldOrderName +'] desc'

        end

        else

        begin         set @strTmp = ' >(select max '

            set @strOrder = ' order by [' + @fldOrderName +'] asc'

        end

        --设置主查询临时表

        if(@strWhere!='')

            set @strTable='(select top '+STR(@limitCount)+' '+@fldNames+' from ['+@tblName+'] where '+@strWhere+') as tmpTable'

        else

            set @strTable='(select top '+STR(@limitCount)+' '+@fldNames+' from ['+@tblName+']) as tmpTable'

        --设置总数查询语句和主语句

        set @strOrderSQL=N'select @totalCountOut=count(1) from '+@strTable

        set @strOutParam=N'@totalCountOut int output'

        if (@pageIndex = 1)

            set @strSQL = 'select top ' + str(@pageSize) +' '+@fldNames+ ' from ' + @strTable + @strOrder

        else

            set @strSQL = 'select top ' + str(@PageSize) +' '+@fldNames+ ' from ' + @strTable + ' where [' + @fldOrderName + ']' + @strTmp + '(['+ @fldOrderName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldOrderName + '] from ' + @strTable + @strOrder + ') as tblTmp) ' + @strOrder

        exec sp_executesql @strOrderSQL,@strOutParam,@totalCountOut=@totalCount output

        exec sp_executesql @strSQL     end

  • 相关阅读:
    HDU
    P2458 [SDOI2006]保安站岗[树形dp]
    POJ 2155 Matrix[树状数组+差分]
    P1281 书的复制[二分]
    【清北学堂】广州OI学习游记
    P1736 创意吃鱼法[二维dp]
    P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
    P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]
    P1273 有线电视网[分组背包+树形dp]
    windows下安装MySQL
  • 原文地址:https://www.cnblogs.com/yuanye0918/p/3771913.html
Copyright © 2011-2022 走看看