SELECT * FROM  
(
SELECT TOP 8 * FROM
(
SELECT TOP 16 *
FROM Articles
ORDER BY PubTime DESC
)
ORDER BY PubTime ASC
)
ORDER BY PubTime DESC
这个的想法就是“掐头去尾”,要取出按时间降序排列的第9到第16条这8条记录,首先先按时间降序取出前16条记录,由于这16条记录的后8条正是我们需要的记录,所以再对这16条记录按时间升序排列,取出前8条,这8条正是我们需要的记录,但是排序不对,最后再对这8条记录按时间降序排列,正好得到需要的记录和排序方式。
其实,取出中间数据的方法还有很多,这和分页的原理差不多,在网上还发现使用存储过程来实现的,这一种方式是以 ASP.NET Forum 为代表的、“临时表”方法:即在存储过程中建立一个临时表,该临时表包含一个序号字段(1,2,3,....)以及表的主键(其他能够唯一确定一行记录的字段也是可以的)字段。存储过程可能如下:
CREATE Procedure GetAllArticles_Paged
(
@PageIndex int,
@PageSize int,
@TotalRecords out int,
@TotalPages out int
)
AS
  
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
-- Set the page bounds
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
-- Create a temp table to store the select results
CREATE TABLE #tmp
(
RecNo int IDENTITY (1, 1) NOT NULL,
ArticleID int
)
INSERT INTO #tmp
SELECT [ID]
FROM Articles
ORDER BY PubTime DESC
SELECT A.*
FROM Articles A (nolock), #tmp T
WHERE A.ID = T.ArticleID AND
T.RecNo > @PageLowerBound AND
T.RecNo < @PageUpperBound
ORDER BY T.RecNo
GO
http://www.hwxz.com/ShowContent.aspx?ArticleID=20535
(
SELECT TOP 8 * FROM
(
SELECT TOP 16 *
FROM Articles
ORDER BY PubTime DESC
)
ORDER BY PubTime ASC
)
ORDER BY PubTime DESC
这个的想法就是“掐头去尾”,要取出按时间降序排列的第9到第16条这8条记录,首先先按时间降序取出前16条记录,由于这16条记录的后8条正是我们需要的记录,所以再对这16条记录按时间升序排列,取出前8条,这8条正是我们需要的记录,但是排序不对,最后再对这8条记录按时间降序排列,正好得到需要的记录和排序方式。
其实,取出中间数据的方法还有很多,这和分页的原理差不多,在网上还发现使用存储过程来实现的,这一种方式是以 ASP.NET Forum 为代表的、“临时表”方法:即在存储过程中建立一个临时表,该临时表包含一个序号字段(1,2,3,....)以及表的主键(其他能够唯一确定一行记录的字段也是可以的)字段。存储过程可能如下:
CREATE Procedure GetAllArticles_Paged
(
@PageIndex int,
@PageSize int,
@TotalRecords out int,
@TotalPages out int
)
AS
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
-- Set the page bounds
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
-- Create a temp table to store the select results
CREATE TABLE #tmp
(
RecNo int IDENTITY (1, 1) NOT NULL,
ArticleID int
)
INSERT INTO #tmp
SELECT [ID]
FROM Articles
ORDER BY PubTime DESC
SELECT A.*
FROM Articles A (nolock), #tmp T
WHERE A.ID = T.ArticleID AND
T.RecNo > @PageLowerBound AND
T.RecNo < @PageUpperBound
ORDER BY T.RecNo
GO
http://www.hwxz.com/ShowContent.aspx?ArticleID=20535
                    
                
                
            
        
浙公网安备 33010602011771号