發布一個高效的資料分頁的預存程序 可以輕鬆應付百萬資料

來源:互聯網
上載者:User
預存程序|分頁|資料

CREATE PROCEDURE pageTest  --用於翻頁的測試
--需要把排序欄位放在第一列

 (
  @FirstID nvarchar(20)=null,  --當前頁面裡的第一條記錄的排序欄位的值
  @LastID nvarchar(20)=null,  --當前頁面裡的最後一條記錄的排序欄位的值
  @isNext bit=null,    --true 1 :下一頁;false 0:上一頁
  @allCount int output,   --返回總記錄數
  @pageSize int output,   --返回一頁的記錄數
  @CurPage int     --頁號(第幾頁)0:第一頁;-1最後一頁。
  )

AS

if @CurPage=0
 begin
  --統計總記錄數
  select @allCount=count(ProductId) from Product_test
  
  set @pageSize=10
  --返回第一頁的資料
  select top 10
   ProductId,
   ProductName,
   Introduction  
   from Product_test order by ProductId
 end

else if @CurPage=-1

 select * from
  (select top 10 ProductId,
   ProductName,
   Introduction

  from Product_test order by ProductId desc ) as aa 
  order by ProductId
else

 begin
  if @isNext=1
   --翻到下一頁
   select top 10 ProductId,
   ProductName,
   Introduction
  from Product_test where ProductId > @LastID order by ProductId
  
  
  else
   --翻到上一頁
   select * from
    (select top 10 ProductId,
   ProductName,
   Introduction
  from Product_test where ProductId < @FirstID  order by ProductId desc) as bb order by ProductId
 end
 

百萬資料翻頁就像100條資料一樣!

http://www.jyklzz.net/web/jyk/index.asp 這裡有詳細的說明

 

 



聯繫我們

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