原理:
PageSize, PageIndex;
升序 order by a asc
a的 值 大于, 上一页数据 最大的 a值。
降序 order by a desc
a的 值 小于, 上一页数据 最小的 a值。
代码实例
USE [SZSQ]
GO
/****** Object: StoredProcedure [dbo].[dnt_gettopiclistbytypedatecph] Script Date: 09/12/2014 15:18:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[dnt_PageSplb]
@pagesize int,
@pageindex int,
@startnum int,
@condition varchar(1000),
@orderby varchar(100),
@ascdesc int
AS
DECLARE @strsql varchar(5000)
DECLARE @sorttype varchar(5)
IF @ascdesc=0
SET @sorttype=‘ASC‘
ELSE
SET @sorttype=‘DESC‘
IF @pageindex = 1
BEGIN
SET @strsql = ‘SELECT TOP ‘ + STR(@pagesize) +‘ [sid],[sname],[sauthor],[sdate],[simg],[slink],[spjs],[slbdl],[slbps],[slbgjw],[slbql],[slbjs],[sbfcs],[splcs],[szdcs],[scdcs],[ssfsx]
from [dnt_splb] where [ssfsx]=1‘[email protected]+‘ ORDER BY ‘[email protected]+‘ ‘[email protected]
END
ELSE
IF @sorttype=‘DESC‘
BEGIN
SET @strSQL = ‘SELECT TOP ‘ + STR(@pagesize) +‘ [sid],[sname],[sauthor],[sdate],[simg],[slink],[spjs],[slbdl],[slbps],[slbgjw],[slbql],[slbjs],[sbfcs],[splcs],[szdcs],[scdcs],[ssfsx]
FROM [dnt_splb]
WHERE dnt_splb.[‘[email protected]+‘] < (SELECT min(tblTmp.[‘[email protected]+‘]) FROM (SELECT TOP ‘ + STR((@pageindex-1)*@[email protected]) + ‘ [dnt_splb].[‘[email protected]+‘] FROM [dnt_splb] WHERE [ssfsx]=1‘[email protected]+‘ ORDER BY [dnt_splb].‘[email protected]+‘ ‘[email protected]+‘)AS tblTmp )
AND [ssfsx]=1
ORDER BY [dnt_splb].‘[email protected]+‘ ‘[email protected]
END
ELSE
BEGIN
SET @strSQL = ‘SELECT TOP ‘ + STR(@pagesize) +‘ [sid],[sname],[sauthor],[sdate],[simg],[slink],[spjs],[slbdl],[slbps],[slbgjw],[slbql],[slbjs],[sbfcs],[splcs],[szdcs],[scdcs],[ssfsx]
FROM [dnt_splb]
WHERE dnt_splb.[‘[email protected]+‘] > (SELECT min(tblTmp.[‘[email protected]+‘]) FROM (SELECT TOP ‘ + STR((@pageindex-1)*@[email protected]) + ‘ [dnt_splb].[‘[email protected]+‘] FROM [dnt_splb] WHERE [ssfsx]=1‘[email protected]+‘ ORDER BY [dnt_splb].‘[email protected]+‘ ‘[email protected]+‘)AS tblTmp )
AND [ssfsx]=1
ORDER BY [dnt_splb].‘[email protected]+‘ ‘[email protected]
END
print @strSQL
EXEC(@strsql)