首頁  >  文章  >  資料庫  >  三种常用数据库(Oracle、MySQL、SQLServer)的分页之SQLServer

三种常用数据库(Oracle、MySQL、SQLServer)的分页之SQLServer

WBOY
WBOY原創
2016-06-07 15:51:521185瀏覽

环境 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 in

环境

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);


 

运行效果截图

 

三种常用数据库(Oracle、MySQL、SQLServer)的分页之SQLServer

 

小技巧

 

快速插入数据:

insert into test(name) select name from test;

 

总结语法

 

SELECT TOP页大小 *

FROM TestTable

WHERE (ID NOT IN

          (SELECT TOP页大小 * 页数 id

         FROM

         ORDER BY id))

ORDER BY ID

 

select top pageSize * from tableName where id not in (select top  pageSize * (pageNow - 1) id from tableName);

 

pageNow:当前第几页

 

pageSize:每页显示的记录数

 

参考资料

 

http://android.blog.51cto.com/268543/54310

 

http://bbs.csdn.net/topics/32008815

 

 

 

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn