sql存储过程分页



exec [dbo].[AJ_CTFX_Page] 10,1
alter PROCEDURE [dbo].[AJ_CTFX_Page]
--@FrequentFlyerName NVARCHAR(128),
--@PapersNumber NVARCHAR(256),
--@CustomerId UNIQUEIDENTIFIER,
@PageSize INT = 10,
@PageIndex INT = 0,
@TotalRecords INT = 0 OUTPUT
AS
BEGIN
SET NOCOUNT ON


DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
DECLARE @RowsToReturn int

SET @RowsToReturn = @PageSize * @PageIndex
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1

CREATE TABLE #PageIndex 
(
IndexId INT IDENTITY (1, 1) NOT NULL,
ID INT NOT NULL
)


INSERT INTO #PageIndex ( ID )
SELECT ID
FROM [dbo].[Papers_AnswerDetailed] WITH(NOLOCK)
WHERE
(
( [IsRight]=1 )
--AND
--( @FrequentFlyerName IS NULL OR ( PATINDEX('%'+@FrequentFlyerName+'%',Name)>0 OR PATINDEX('%'+@FrequentFlyerName+'%',PinyinName)>0) )
--AND
--( @PapersNumber IS NULL OR [PapersNumber]=@PapersNumber )
--AND
--( @CustomerId IS NULL OR [CreateBy]=@CustomerId )
--AND
--( ParentFlyerId = 0 )
)
ORDER BY [ID] DESC

SELECT @TotalRecords=COUNT(IndexId) FROM #PageIndex
SET ROWCOUNT @RowsToReturn

SELECT
[ad].[TestID],[ad].[AnswerJson],[t].[AnswerCount],[t].[AnswerErrorCount],[t].[AnswerRightCount],[t].[AnswerJson],[t].[Caption]
FROM
[dbo].[Papers_AnswerDetailed] [ad]
INNER JOIN #PageIndex [pi] ON [ad].ID=[pi].ID
INNER JOIN [dbo].[Papers_Test] [t] ON [ad].[TestID]=[t].[ID]
WHERE
[pi].IndexId > @PageLowerBound
AND
[pi].IndexId < @PageUpperBound
ORDER BY ad.ID DESC

SET ROWCOUNT 0


DROP TABLE #PageIndex

END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值