sql分頁查詢,sql分頁操作

 2023-11-18 阅读 11 评论 0

摘要:看到了網上關于分頁的講解 ?對最快的分頁語句做了測試 還別說速度真快 總共6w條數據 速度確實so 快 前提是id是主鍵 或者是索引 declare @page int;--頁數 declare @PageSize int;--單頁顯示數select @page=1; select @pageSize=1000;select top (

看到了網上關于分頁的講解 ?對最快的分頁語句做了測試 還別說速度真快 總共6w條數據 速度確實so 快

前提是id是主鍵 或者是索引

declare @page int;--頁數
declare @PageSize int;--單頁顯示數select @page=1;
select @pageSize=1000;select top (@pageSize) * from rdrecord rd
left join Vendor Ven on rd.cVenCode =Ven.cVenCode
where 
(id > (select isnull(max(id),0) from ( select top ((@page-1)*@pageSize) id from rdrecord order by id ) RDSID)
)
order by id 

存儲存過程
--sql分頁存儲過程
--@sqlstr  查詢語句
--@currentpage 當前頁碼
--@pagesize每頁信息數
--返回值
---1、記錄數
---2、符合條件的記錄集
CREATE procedure [dbo].[PagingQuery]
@sqlstr nvarchar(4000), --查詢字符串
@currentpage int, --第N頁
@pagesize int, --每頁行數
@allrecords int OUTPUT  --返回的總記錄數
as
set nocount on
declare @P1 int, --P1是游標的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
set @allrecords=@rowcount
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount offGO

?

  

轉載于:https://www.cnblogs.com/SoftWareIe/p/5458868.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/174722.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息