实现sqlserver分页查询语句
sqlServer:
一次查询,数据库只返回一页的数据。而不是取出所有的数据。
pagesize: 每页显示记录数
cureentpage:当前页数
select * from ( select TOP pagesize * from ( SELECT TOP pagesize*cureentpage * from my_table ORDER BY id ASC ) as amyTable ORDER BY id DESC ) as bmyTable ORDER BY id ASC
假设共有有8条记录,每页显示3条记录,id 为3,6,7,37
第一页(首页)
select * from ( select TOP 3 * from ( SELECT TOP 3*1 * from my_table ORDER BY id ASC ) as amyTable ORDER BY id DESC ) as bmyTable ORDER BY id ASC
第二页
select * from ( select TOP 3 * from ( SELECT TOP 3*2 * from my_table ORDER BY id ASC ) as amyTable ORDER BY id DESC ) as bmyTable ORDER BY id ASC
第三页(尾页)
select * from ( select TOP 8-2*3 * from ( SELECT TOP 8-3*2 * from my_table ORDER BY id ASC ) as amyTable ORDER BY id DESC ) as bmyTable ORDER BY id ASC
相关文档:
SQL Server 2000
Installing SQL Server 2000 (E文)
http://msdn.microsoft.com/en-us/library/aa299042(SQL.80).aspx
SQL Server 2000补丁
Microsoft SQL Server 2000 Service Pack 3a
http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=90DCD52C-0488-4E46-AFBF-ACACE536 ......
create database DB
use DB
--专业表
create table major
(spno char(5) not null primary key,
spname varchar(20) not null,
pno char(2) )
--学生表
create table student
(sno char(7) not null primary key,
sname varchar(20) not null,
ssex char(2) not null,
sag ......
游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力。我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许用户对指定位置的数据进行处理。
1.游标的组成
......
DATEDIFF(datepart, startdate, enddate)
Datepart Abbreviations
year yy, yyyy
quarter qq, q
month mm, m
......