2分法分頁預存程序指令碼執行個體_MsSql

來源:互聯網
上載者:User

需要說明的是:這個預存程序參數比較多,我再實際使用中又在外面單獨寫了一個類,頁面調用直接調用封裝的類,方法有很多,主要是思路,大家可以參考下。

代碼修改集中在類似

複製代碼 代碼如下:

if @Sort=0
set @strTmp = @strTmp + '<(select min('
 else
set @strTmp = @strTmp + '>(select max('

另外94行主要是配合我自己寫的類,顯示記錄條數分頁數等資訊,如果不需要就去掉。

複製代碼 代碼如下:

  1ALTER PROCEDURE [dbo].[proc_ListPage]
  2(
  3 @tblName     nvarchar(200),        ----要顯示的表或多個表的串連
  4 @fldName     nvarchar(500) = '*',    ----要顯示的欄位列表
  5 @pageSize    int = 10,        ----每頁顯示的記錄個數
  6 @page        int = 1,        ----要顯示那一頁的記錄
  7 @fldSort    nvarchar(200) = null,    ----排序欄位列表或條件
  8 @Sort        bit = 0,        ----排序方法,0為升序,1為降序(如果是多欄位排列Sort指代最後一個排序欄位的排列順序(最後一個排序欄位不加排序標記)--程式傳參如:' SortA Asc,SortB Desc,SortC ')
  9 @strCondition    nvarchar(1000) = null,    ----查詢條件,不需where
 10 @ID        nvarchar(150),        ----主表的主鍵
 11 @Dist      bit = 0,           ----是否添加查詢欄位的 DISTINCT 預設0不添加/1添加
 12 @pageCount    int = 1 output,            ----查詢結果分頁後的總頁數
 13 @Counts    int = 1 output                ----查詢到的記錄數
 14 )
 15 AS
 16 SET NOCOUNT ON
 17 Declare @sqlTmp nvarchar(1000)        ----存放動態產生的SQL語句
 18 Declare @strTmp nvarchar(1000)        ----存放取得查詢結果總數的查詢語句
 19 Declare @strID     nvarchar(1000)        ----存放取得查詢開頭或結尾ID的查詢語句
 20
 21 Declare @strSortType nvarchar(10)    ----資料定序A
 22 Declare @strFSortType nvarchar(10)    ----資料定序B
 23
 24 Declare @SqlSelect nvarchar(50)         ----對含有DISTINCT的查詢進行SQL構造
 25 Declare @SqlCounts nvarchar(50)          ----對含有DISTINCT的總數查詢進行SQL構造
 26
 27
 28 if @Dist  = 0
 29 begin
 30     set @SqlSelect = 'select '
 31     set @SqlCounts = 'Count(0)'
 32 end
 33 else
 34 begin
 35     set @SqlSelect = 'select distinct '
 36     set @SqlCounts = 'Count(DISTINCT '+@ID+')'
 37 end
 38
 39
 40 if @Sort=0
 41 begin
 42     set @strFSortType=' ASC '
 43     set @strSortType=' DESC '
 44 end
 45 else
 46 begin
 47     set @strFSortType=' DESC '
 48     set @strSortType=' ASC '
 49 end
 50
 51
 52
 53 --------產生查詢語句--------
 54 --此處@strTmp為取得查詢結果數量的語句
 55 if @strCondition is null or @strCondition=''     --沒有設定顯示條件
 56 begin
 57     set @sqlTmp =  @fldName + ' From ' + @tblName
 58     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
 59     set @strID = ' From ' + @tblName
 60 end
 61 else
 62 begin
 63     set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
 64     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
 65     set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
 66 end
 67
 68 ----取得查詢結果總數量-----
 69 exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
 70 declare @tmpCounts int
 71 if @Counts = 0
 72     set @tmpCounts = 1
 73 else
 74     set @tmpCounts = @Counts
 75
 76     --取得分頁總數
 77     set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
 78
 79     /**//**當前頁大於總頁數 取最後一頁**/
 80     if @page>@pageCount
 81         set @page=@pageCount
 82
 83     --/*-----資料分頁2分處理-------*/
 84     declare @pageIndex int --總數/頁大小
 85     declare @lastcount int --總數%頁大小
 86
 87     set @pageIndex = @tmpCounts/@pageSize
 88     set @lastcount = @tmpCounts%@pageSize
 89     if @lastcount > 0
 90         set @pageIndex = @pageIndex + 1
 91     else
 92         set @lastcount = @pagesize
 93
 94 --為配合顯示
 95 set nocount off
 96 select @page curpage,@pageSize pagesize,@pageCount countpage,@tmpCounts [Rowcount]
 97 set nocount on
 98
 99  --//***顯示分頁
100     if @strCondition is null or @strCondition=''     --沒有設定顯示條件
101     begin
102         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分資料處理
103             begin
104                 if @page=1
105                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName                       
106                         +' order by '+ @fldSort +' '+ @strFSortType
107                 else
108                 begin                   
109                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
110                         +' where '+@ID
111                     if @Sort=0
112                        set @strTmp = @strTmp + '>(select max('
113                     else
114                        set @strTmp = @strTmp + '<(select min('
115                     set @strTmp = @strTmp + @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
116                         +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
117                         +' order by '+ @fldSort +' '+ @strFSortType
118                 end   
119             end
120         else
121            
122             begin
123             set @page = @pageIndex-@page+1 --後半部分資料處理
124                 if @page <= 1 --最後一頁資料顯示           
125                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(40))+' '+ @fldName+' from '+@tblName
126                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
127                 else
128                     begin
129                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
130                         +' where '+@ID
131                         if @Sort=0
132                            set @strTmp=@strTmp+' <(select min('
133                         else
134                            set @strTmp=@strTmp+' >(select max('
135  set @strTmp=@strTmp+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
136                         +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
137                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
138                    end
139             end
140
141     end
142
143     else --有查詢條件
144     begin
145         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分資料處理
146         begin
147                 if @page=1
148                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName                       
149                         +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
150                 else
151                 begin                   
152                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
153                         +' where '+@ID
154                     if @Sort=0
155                        set @strTmp = @strTmp + '>(select max('
156                     else
157                        set @strTmp = @strTmp + '<(select min('
158
159                  set @strTmp = @strTmp + @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
160                         +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
161                         +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
162                 end           
163         end
164         else
165         begin
166             set @page = @pageIndex-@page+1 --後半部分資料處理
167             if @page <= 1 --最後一頁資料顯示
168                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(40))+' '+ @fldName+' from '+@tblName
169                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                    
170             else
171                   begin
172                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
173                         +' where '+@ID
174                     if @Sort=0
175                        set @strTmp = @strTmp + '<(select min('
176                     else
177                        set @strTmp = @strTmp + '>(select max('
178                set @strTmp = @strTmp + @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
179                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
180                         +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
181                  end             
182         end   
183  
184     end
185
186 ------返回查詢結果-----
187 SET NOCOUNT off
188 exec sp_executesql @strTmp
189 print @strTmp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.