最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SqlServer 2000、2005分頁存儲(chǔ)過程整理

 更新時(shí)間:2015年02月15日 10:20:04   投稿:junjie  
這篇文章主要介紹了SqlServer 2000、2005分頁存儲(chǔ)過程整理,本文給出了4個(gè)實(shí)現(xiàn)版本,還給出二分分頁過程,需要的朋友可以參考下
@RecordCount int output,@ReturnCount bit,@QueryStr nvarchar(1000)='table1',--表名、視圖名、查詢語句@PageSize int=20, --每頁的大小(行數(shù))@PageCurrent int=2, --要顯示的頁 從0開始@FdShow nvarchar (2000)='*', --要顯示的字段列表@IdentityStr nvarchar (100)='id', --主鍵@WhereStr nvarchar (2000)='1=1',@FdOrder nvarchar(100)='desc' --排序 只能取desc或者ascasset nocount ondeclare@sql nvarchar(2000)if @WhereStr = '' begin set @WhereStr = '1=1'endif @ReturnCount=1 begin declare @tsql nvarchar(200) set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount outputendif @PageCurrent = 0 begin set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrderendelse begin if upper(@FdOrder) = 'DESC' begin set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc' end else begin set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc' endend--print @sqlexecute(@sql)

二分分頁過程:

--/*-----存儲(chǔ)過程 分頁處理 孫偉 2005-03-28創(chuàng)建 -------*/--/*-----存儲(chǔ)過程 分頁處理 浪塵 2008-9-1修改----------*/--/*----- 對(duì)數(shù)據(jù)進(jìn)行了2分處理使查詢前半部分?jǐn)?shù)據(jù)與查詢后半部分?jǐn)?shù)據(jù)性能相同 -------*/alter PROCEDURE proc_paged_2part_selectMax(@tblName   nvarchar(200),    ----要顯示的表或多個(gè)表的連接@fldName   nvarchar(500) = '*',  ----要顯示的字段列表@pageSize  int = 10,    ----每頁顯示的記錄個(gè)數(shù)@page    int = 1,    ----要顯示那一頁的記錄@fldSort  nvarchar(200) = null,  ----排序字段列表或條件@Sort    bit = 0,    ----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個(gè)排序字段的排列順序(最后一個(gè)排序字段不加排序標(biāo)記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')@strCondition  nvarchar(1000) = null,  ----查詢條件,不需where@ID    nvarchar(150),    ----主表的主鍵@Dist         bit = 0,      ----是否添加查詢字段的 DISTINCT 默認(rèn)0不添加/1添加@pageCount  int = 1 output,      ----查詢結(jié)果分頁后的總頁數(shù)@Counts  int = 1 output        ----查詢到的記錄數(shù))ASSET NOCOUNT ONDeclare @sqlTmp nvarchar(1000)    ----存放動(dòng)態(tài)生成的SQL語句Declare @strTmp nvarchar(1000)    ----存放取得查詢結(jié)果總數(shù)的查詢語句Declare @strID   nvarchar(1000)    ----存放取得查詢開頭或結(jié)尾ID的查詢語句Declare @strSortType nvarchar(10)  ----數(shù)據(jù)排序規(guī)則ADeclare @strFSortType nvarchar(10)  ----數(shù)據(jù)排序規(guī)則BDeclare @SqlSelect nvarchar(50)     ----對(duì)含有DISTINCT的查詢進(jìn)行SQL構(gòu)造Declare @SqlCounts nvarchar(50)     ----對(duì)含有DISTINCT的總數(shù)查詢進(jìn)行SQL構(gòu)造declare @timediff datetime --耗時(shí)測(cè)試時(shí)間差select @timediff=getdate()if @Dist = 0begin  set @SqlSelect = 'select '  set @SqlCounts = 'Count(*)'endelsebegin  set @SqlSelect = 'select distinct '  set @SqlCounts = 'Count(DISTINCT '+@ID+')'endif @Sort=0begin  set @strFSortType=' ASC '  set @strSortType=' DESC 'endelsebegin  set @strFSortType=' DESC '  set @strSortType=' ASC 'end--------生成查詢語句----------此處@strTmp為取得查詢結(jié)果數(shù)量的語句if @strCondition is null or @strCondition=''   --沒有設(shè)置顯示條件begin  set @sqlTmp = @fldName + ' From ' + @tblName  set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName  set @strID = ' From ' + @tblNameendelsebegin  set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition  set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition  set @strID = ' From ' + @tblName + ' where (1>0) ' + @strConditionend----取得查詢結(jié)果總數(shù)量-----exec sp_executesql @strTmp,N'@Counts int out ',@Counts outdeclare @tmpCounts intif @Counts = 0  set @tmpCounts = 1else  set @tmpCounts = @Counts  --取得分頁總數(shù)  set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize  /**//**//**//**當(dāng)前頁大于總頁數(shù) 取最后一頁**/  if @page>@pageCount    set @page=@pageCount  --/*-----數(shù)據(jù)分頁2分處理-------*/  declare @pageIndex int --總數(shù)/頁大小  declare @lastcount int --總數(shù)%頁大小  set @pageIndex = @tmpCounts/@pageSize  set @lastcount = @tmpCounts%@pageSize  if @lastcount > 0    set @pageIndex = @pageIndex + 1  else    set @lastcount = @pagesize  --//***顯示分頁  if @strCondition is null or @strCondition=''   --沒有設(shè)置顯示條件  begin    if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2  --前半部分?jǐn)?shù)據(jù)處理      begin        if @page=1          set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' order by '+ @fldSort +' '+ @strFSortType        else        begin          if @Sort=1          begin          set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName            +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'            +' order by '+ @fldSort +' '+ @strFSortType          end          else          begin          set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName            +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'            +' order by '+ @fldSort +' '+ @strFSortType          end        end      end    else      begin      set @page = @pageIndex-@page+1 --后半部分?jǐn)?shù)據(jù)處理        if @page <= 1 --最后一頁數(shù)據(jù)顯示          set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType        else          if @Sort=1          begin          set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName            +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'            +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType          end          else          begin          set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' <(select min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName            +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'            +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType          end      end  end  else --有查詢條件  begin    if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2  --前半部分?jǐn)?shù)據(jù)處理    begin        if @page=1          set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType        else if(@Sort=1)        begin          set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName            +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'            +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType        end        else        begin          set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName            +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'            +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType        end    end    else    begin      set @page = @pageIndex-@page+1 --后半部分?jǐn)?shù)據(jù)處理      if @page <= 1 --最后一頁數(shù)據(jù)顯示          set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType      else if(@Sort=1)          set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName            +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'            +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType      else          set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName            +' where '+@ID+' <(select min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName            +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'            +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType    end  end------返回查詢結(jié)果-----exec sp_executesql @strTmpselect datediff(ms,@timediff,getdate()) as 耗時(shí)--print @strTmpSET NOCOUNT OFFGO


相關(guān)文章

最新評(píng)論

法库县| 两当县| 正阳县| 嵊州市| 融水| 伊宁市| 南平市| 仙居县| 讷河市| 施甸县| 泰安市| 巴彦淖尔市| 石泉县| 绥化市| 定安县| 赤壁市| 本溪| 尚义县| 潍坊市| 石渠县| 门源| 庆安县| 大石桥市| 日土县| 铜鼓县| 无棣县| 海晏县| 闵行区| 五台县| 东乡族自治县| 永仁县| 西乌| 社会| 伽师县| 理塘县| 枣阳市| 望城县| 漠河县| 房产| 文化| 康定县|