環境
SQLServer 2008 R2
問題
SQLServer分頁
解決
use test; --創建測試表 create table test ( id int primary key identity, name varchar(20) not null ); --插入數據 insert into test(name) values('test1'); insert into test(name) values('test2'); insert into test(name) values('test3'); insert into test(name) values('test4'); insert into test(name) values('test5'); insert into test(name) values('test6'); insert into test(name) values('test7'); insert into test(name) values('test8'); insert into test(name) values('test9'); insert into test(name) values('test10'); select * from test; --執行分頁 SELECT TOP 10 id,name FROM test WHERE id NOT IN (SELECT TOP (10 * 0) id FROM test ORDER BY id) ORDER BY ID select id,name from test; --select top pageSize * from tableName where id not in (select top pageSize * (pageNow - 1) id from tableName);
運行效果截圖