欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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

程序员文章站 2024-01-29 12:49:46
...

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