簡單實用asp.net mssql萬能分頁程式

來源:互聯網
上載者:User

簡單實用asp教程.net mssql萬能分頁程式
一、比較萬能的分頁:

sql代碼:

view sourceprint?1 select top 每頁顯示的記錄數 * from topic where id not in

2  (select top (當前的頁數-1)×每頁顯示的記錄數 id from topic order by id desc) 

3  order by id desc


需要注意的是在access中不能是top 0,所以如果資料只有一頁的話就得做判斷了。。

二、sql2005中的分頁代碼:

sql代碼:

view sourceprint?1 --講查詢出來的結果集作為一張暫存資料表 

2 with temptable as ( 

3   select row_number() over (order by id desc) as rownum, * from tablename 

4 ) 

5 --從暫存資料表中取得指定行號區間的行 

6 select * from temptable where rownum between @startindex and @endindex


註:row_number() over (order by id desc)為sql2005新增函數,表示取出每一列的行號

 

三、分頁預存程序sql代碼:

sql代碼:

view sourceprint?01 set ansi_nulls on

02 go 

03 set quoted_identifier on

04 go 

05 -- ============================================= 

06 -- author:      牛腩 

07 -- create date: 2009-07-22 12:41 

08 -- description: 分頁,用到了row_number() 

09 -- ============================================= 

10 alter procedure [dbo].[proc_showpage] 

11 @tblname   varchar(255),       -- 表名 

12 @strgetfields varchar(1000) = '*', -- 需要返回的列,預設* 

13 @strorder varchar(255)='',      -- 排序的欄位名,必填 

14 @strordertype varchar(10)='asc', -- 排序的方式,預設asc 

15 @pagesize   int = 10,          -- 頁尺寸,預設10 

16 @pageindex int = 1,           -- 頁碼,預設1 

17 @strwhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where) 

18 as

19   

20 declare @strsql   varchar(5000) 

21   

22 if @strwhere !=''

23 set @strwhere=' where '+@strwhere 

24   

25 set @strsql= 

26 'select * from ('+ 

27     'select row_number() over (order by '+@strorder+' '+@strordertype+') as pos,'+@strgetfields+' '+ 

28     'from ['+@tblname+'] '+@strwhere+ 

29 ') as sp where pos between '+str((@pageindex-1)*@pagesize+1)+' and '+str(@pageindex*@pagesize) 

30   

31 exec (@strsql) 

32 print @strsql  -- 測試用,可在查詢的時候看到產生的sql語句

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.