通用分頁(yè)存儲(chǔ)過(guò)程(原創(chuàng))

           這是我項(xiàng)目中使用的一個(gè)分頁(yè)存儲(chǔ)過(guò)程,具有很強(qiáng)的通用性。配合前臺(tái)ASP.NET使用50萬(wàn)條數(shù)據(jù)基本感不到延遲。數(shù)據(jù)庫(kù)為SQLServer2000。

          1.分頁(yè)存儲(chǔ)過(guò)程

          CREATE   procedure pagination

           @str_sql           varchar(1000) = '*',     -- 執(zhí)行的SQL 不含Order by 內(nèi)容  
           @str_orderfield    varchar(255)='''',       -- 排序的字段名 
           @page_size         int = 10,                     -- 頁(yè)大小 
           @page_index        int = 0,                      -- 頁(yè)碼
           @order_type        int,                           -- 設(shè)置排序類型, 非 -1 值則降序 
           @total_count       int   output                 -- 返回記錄總數(shù), 非 0 值則返回 
          as

          ---------------------
          -- 獲取指定頁(yè)的數(shù)據(jù)--
          ---------------------

          declare @strsql   varchar(5000)              -- 主語(yǔ)句
          declare @strtmp   varchar(5000)             -- 臨時(shí)變量
          declare @strorder varchar(400)              -- 排序字串
          declare @cruRow   int                            -- 當(dāng)前行號(hào)
           

          --執(zhí)行總數(shù)統(tǒng)計(jì)
          exec getRowCount @str_sql,@total_count output

          set @strtmp =  ' select * from ' +
                  '      (select top ' + convert(varchar(10),@page_size) + ' * from ' +
                  '         (select top ' + convert(varchar(10),(@page_index + 1) * @page_size)  +' * from '+        -- N+1頁(yè)
                  '            ('+ @str_sql +') Src '

          --排序方向
          if @order_type !=0
           begin
           set @strsql= @strtmp +
                 '          order by @str_orderfield asc) a ' +
                 '       order by @str_orderfield desc)b' +
                        ' order by @str_orderfield asc'
           end
          else
           begin
           set @strsql= @strtmp +
                 '          order by @str_orderfield desc) a ' +
                 '       order by  @str_orderfieldasc)b' +
                        ' order by  @str_orderfield desc'
           end

          exec (@strsql)

          GO

          ----------------------------------------------------------------------------

          2.分頁(yè)存儲(chǔ)過(guò)程執(zhí)行中用到的行數(shù)統(tǒng)計(jì)

          create  procedure getRowCount
                 @sql    nvarchar(2000),
                 @count  int output
          as
          begin

          --------------------
          -- 獲取數(shù)據(jù)總行數(shù) --
          --------------------

            declare @tmpsql nvarchar(2000)
            set @tmpsql='select @count=count(*)  from ('+ @sql +') a'

            execute sp_executesql @tmpsql,N'@count int output',@count output
           
          end

          GO

          posted on 2008-01-10 17:08 靈! 閱讀(520) 評(píng)論(0)  編輯  收藏 所屬分類: Java專題-分頁(yè)技術(shù)


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計(jì)

          隨筆分類

          隨筆檔案

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 电白县| 岳池县| 马关县| 海原县| 南川市| 安达市| 新邵县| 嘉义市| 海淀区| 泊头市| 滦平县| 和顺县| 承德县| 安泽县| 台南县| 云林县| 日土县| 资兴市| 尉氏县| 长兴县| 涿鹿县| 万宁市| 淮南市| 洛浦县| 兰考县| 嘉峪关市| 苍梧县| 多伦县| 龙川县| 漾濞| 松阳县| 泸州市| 夏河县| 深泽县| 江永县| 娄底市| 武隆县| 合江县| 台北县| 萨嘎县| 泽普县|