码迷,mamicode.com
首页 > 数据库 > 详细

Sql Server三种分页方法

时间:2016-05-12 09:22:18      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME = ‘StuWebName‘)
DROP TABLE StuWebName
GO
CREATE TABLE StuWebName
(
StuId INT IDENTITY(1,1) PRIMARY KEY,
StuName VARCHAR(50) NOT NULL,
StuPassword VARCHAR(50) NOT NULL,
StuSex CHAR(2) DEFAULT(‘男‘),
StuNo INT NOT NULL
)
GO
DECLARE @ID INT
SET @ID=1
WHILE(@ID<100)
BEGIN
    INSERT INTO StuWebName(StuName,StuPassword,StuSex,StuNo)VALUES(‘张三‘+CONVERT(VARCHAR(50),@ID),‘123‘,‘男‘,0902)
    SET @ID=@ID+1
END
GO
select * from StuWebName
GO
-----------------------------------------------------
--几种最常见的排序排序(第一页每页五条数据)
-----------------------------------------------------
--先用row_number函数在每列加序号然后排序
SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY StuId) AS b,* FROM StuWebName) AS a
WHERE b>(2-1)*5 and b<=2*5 
GO
--先查询前10行,然后先倒序在取前5行,最后在倒序(将数据倒回来)
SELECT * FROM (SELECT TOP 5 * FROM
(SELECT TOP 10 * FROM StuWebName ORDER BY StuId ASC) AS T 
ORDER BY StuId DESC)AS T 
ORDER BY StuId ASC
GO
--先查询前6行,让后取StuId的最大值
SELECT TOP 5 *
FROM StuWebName
WHERE (StuId >=
    (SELECT MAX(StuId)
    FROM (SELECT TOP ((5*1)+1) StuId
        FROM StuWebName 
        ORDER BY StuId) AS T)
)
ORDER BY StuId
GO

  

Sql Server三种分页方法

标签:

原文地址:http://www.cnblogs.com/shouce/p/5484278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!