存储过程 分页处理

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