如何用sql語句 實現分頁查詢
方法1:
適用于 SQL Server 2000/2005
SELECT TOP 頁大小 *
FROM table1
WHERE id NOT IN
(
SELECT TOP 頁大小*(頁數-1) id FROM table1 ORDER BY id
)
ORDER BY id
方法2:
適用于 SQL Server 2000/2005
SELECT TOP 頁大小 *
FROM table1
WHERE id >
(
SELECT ISNULL(MAX(id),0)
FROM
(
SELECT TOP 頁大小*(頁數-1) id FROM table1 ORDER BY id
) A
)
ORDER BY id
方法3:
適用于 SQL Server 2005
SELECT TOP 頁大小 *
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1
) A
WHERE RowNumber >頁大小*(頁數-1)
分頁查詢的sql 語句(參數1,參數2)
你說的應該是 利用SQL的游標存儲過程 來分頁的形式代碼如下:create procedure fenye@sqlstr nvarchar(4000), --查詢字符串 @currentpage int, --第N頁 @pagesize int --每頁行數 as set nocount on declare @P1 int, --P1是游標的id @rowcount int exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output select ceiling(1.0*@rowcount/@pagesize) as 總頁數--,@rowcount as 總行數,@currentpage as 當前頁 set @currentpage=(@currentpage-1)*@pagesize+1 exec sp_cursorfetch @P1,16,@currentpage,@pagesize exec sp_cursorclose @P1 set nocount off 不過這個種存儲過程分頁的方法效率比較差建議你直接用代碼進行分頁或者 利用SELECT TOP分頁代碼:select top 10 * from [order details] where orderid>all(select top 10 orderid from [order details] order by orderid) order by orderid。
分頁查詢的sql 語句(參數1,參數2)
你說的應該是 利用SQL的游標存儲過程 來分頁的形式
代碼如下:
create procedure fenye
@sqlstr nvarchar(4000), --查詢字符串
@currentpage int, --第N頁
@pagesize int --每頁行數
as
set nocount on
declare @P1 int, --P1是游標的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 總頁數--,@rowcount as 總行數,@currentpage as 當前頁
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off
不過這個種存儲過程分頁的方法效率比較差
建議你直接用代碼進行分頁
或者 利用SELECT TOP分頁
代碼:
select top 10 * from [order details]
where orderid>all(select top 10 orderid from [order details] order by orderid)
order by orderid
SQL Server數據庫用sql語句實現分頁查詢 (從M條數據開始,查找N條
create table t1(id int identity primary key,name varchar(16))declare i intset i = 0while(i<30)begin insert into ti values ('name'+i)set i=i+1end-------------------------------例如m=5,N=10select top 10 * from ti where id not in (select top 5 id from ti)。
sql 語句 分頁查詢
方法1:
適用于 SQL Server 2000/2005
SELECT TOP 頁大小 *
FROM table1
WHERE id NOT IN
(
SELECT TOP 頁大小*(頁數-1) id FROM table1 ORDER BY id
)
ORDER BY id
方法2:
適用于 SQL Server 2000/2005
SELECT TOP 頁大小 *
FROM table1
WHERE id >
(
SELECT ISNULL(MAX(id),0)
FROM
(
SELECT TOP 頁大小*(頁數-1) id FROM table1 ORDER BY id
) A
)
ORDER BY id
方法3:
適用于 SQL Server 2005
SELECT TOP 頁大小 *
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1
) A
WHERE RowNumber >; 頁大小*(頁數-1)
這樣可以么?
轉載請注明出處華閱文章網 » 分頁查詢的sql語句