存储过程 分页处理

  1None.gif--/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/
  2None.gif--/*-----存储过程 分页处理  2005-04-21修改 添加Distinct查询功能-------*/
  3None.gif--/*-----存储过程 分页处理  2005-05-18修改 多字段排序规则问题-------*/
  4None.gif--/*-----存储过程 分页处理  2005-06-15修改 多字段排序修改-------*/
  5None.gifALTER PROCEDURE dbo.proc_ListPage
  6None.gif(
  7None.gif@tblName     nvarchar(200),                ----要显示的表或多个表的连接
  8None.gif@fldName     nvarchar(500= '*',        ----要显示的字段列表
  9None.gif@pageSize    int = 1,                    ----每页显示的记录个数
 10None.gif@page        int = 10,                    ----要显示那一页的记录
 11None.gif@pageCount    int = 1 output,           ----查询结果分页后的总页数
 12None.gif@Counts    int = 1 output,              ----查询到的记录数
 13None.gif@fldSort    nvarchar(200= null,        ----排序字段列表或条件
 14None.gif@Sort        bit = 0,                    ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
 15None.gif@strCondition    nvarchar(1000= null,    ----查询条件,不需where
 16None.gif@ID        nvarchar(150),                ----主表的主键
 17None.gif@Dist                 bit = 0           ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
 18None.gif)
 19None.gifAS
 20None.gifSET NOCOUNT ON
 21None.gifDeclare @sqlTmp nvarchar(1000)            ----存放动态生成的SQL语句
 22None.gifDeclare @strTmp nvarchar(1000)            ----存放取得查询结果总数的查询语句
 23None.gifDeclare @strID     nvarchar(1000)       ----存放取得查询开头或结尾ID的查询语句
 24None.gif
 25None.gifDeclare @strSortType nvarchar(10)        ----数据排序规则A
 26None.gifDeclare @strFSortType nvarchar(10)        ----数据排序规则B
 27None.gif
 28None.gifDeclare @SqlSelect nvarchar(50)         ----对含有DISTINCT的查询进行SQL构造
 29None.gifDeclare @SqlCounts nvarchar(50)         ----对含有DISTINCT的总数查询进行SQL构造
 30None.gif
 31None.gif
 32None.gifif @Dist  = 0
 33None.gifbegin
 34None.gif    set @SqlSelect = 'select '
 35None.gif    set @SqlCounts = 'Count(*)'
 36None.gifend
 37None.gifelse
 38None.gifbegin
 39None.gif    set @SqlSelect = 'select distinct '
 40None.gif    set @SqlCounts = 'Count(DISTINCT '+@ID+')'
 41None.gifend
 42None.gif
 43None.gif
 44None.gifif @Sort=0
 45None.gifbegin
 46None.gif    set @strFSortType=' ASC '
 47None.gif    set @strSortType=' DESC '
 48None.gifend
 49None.gifelse
 50None.gifbegin
 51None.gif    set @strFSortType=' DESC '
 52None.gif    set @strSortType=' ASC '
 53None.gifend
 54None.gif
 55None.gif
 56None.gif
 57None.gif--------生成查询语句--------
 58None.gif--此处@strTmp为取得查询结果数量的语句
 59None.gifif @strCondition is null or @strCondition=''     --没有设置显示条件
 60None.gifbegin
 61None.gif    set @sqlTmp =  @fldName + ' From ' + @tblName
 62None.gif    set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
 63None.gif    set @strID = ' From ' + @tblName
 64None.gifend
 65None.gifelse
 66None.gifbegin
 67None.gif    set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
 68None.gif    set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
 69None.gif    set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
 70None.gifend
 71None.gif
 72None.gif----取得查询结果总数量-----
 73None.gifexec sp_executesql @strTmp,N'@Counts int out ',@Counts out
 74None.gifdeclare @tmpCounts int
 75None.gifif @Counts = 0
 76None.gif    set @tmpCounts = 1
 77None.gifelse
 78None.gif    set @tmpCounts = @Counts
 79None.gif
 80None.gif    --取得分页总数
 81None.gif    set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
 82None.gif
 83ExpandedBlockStart.gifContractedBlock.gif    /**//**//**//**当前页大于总页数 取最后一页**/
 84None.gif    if @page>@pageCount
 85None.gif        set @page=@pageCount
 86None.gif
 87None.gif    --/*-----数据分页2分处理-------*/
 88None.gif    declare @pageIndex int --总数/页大小
 89None.gif    declare @lastcount int --总数%页大小 
 90None.gif
 91None.gif    set @pageIndex = @tmpCounts/@pageSize
 92None.gif    set @lastcount = @tmpCounts%@pageSize
 93None.gif    if @lastcount > 0
 94None.gif        set @pageIndex = @pageIndex + 1
 95None.gif    else
 96None.gif        set @lastcount = @pagesize
 97None.gif
 98None.gif    --//***显示分页
 99None.gif    if @strCondition is null or @strCondition=''     --没有设置显示条件
100None.gif    begin
101None.gif        if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
102None.gif            begin 
103None.gif                set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
104None.gif                        +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1as Varchar(20)) +' '+ @ID +' from '+@tblName
105None.gif                        +' order by '+ @fldSort +' '+ @strFSortType+')'
106None.gif                        +' order by '+ @fldSort +' '+ @strFSortType 
107None.gif            end
108None.gif        else
109None.gif            begin
110None.gif            set @page = @pageIndex-@page+1 --后半部分数据处理
111None.gif                if @page <= 1 --最后一页数据显示
112None.gif                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
113None.gif                        +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
114None.gif                else                
115None.gif                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
116None.gif                        +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
117None.gif                        +' order by '+ @fldSort +' '+ @strSortType+')'
118None.gif
119None.gif                        +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
120None.gif            end
121None.gif    end
122None.gif
123None.gif    else --有查询条件
124None.gif    begin
125None.gif        if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
126None.gif        begin 
127None.gif                set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from  '+@tblName
128None.gif                    +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1as Varchar(20)) +' '+ @ID +' from '+@tblName
129None.gif                    +' Where (1>0) ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType+')'
130None.gif                    +' ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType                 
131None.gif        end
132None.gif        else
133None.gif        begin 
134None.gif            set @page = @pageIndex-@page+1 --后半部分数据处理
135None.gif            if @page <= 1 --最后一页数据显示
136None.gif                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
137None.gif                        +' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
138None.gif            else
139None.gif                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
140None.gif                        +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
141None.gif                        +' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+')'
142None.gif                        + @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
143None.gif        end    
144None.gif    end
145None.gif
146None.gif------返回查询结果-----
147None.gifexec sp_executesql @strTmp
148None.gif--print @strTmp
149None.gifSET NOCOUNT OFF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值