2011-04-12 85 views
0

我有存储过程获取表记录。 我是写入存储过程的新手。 如何将分页添加到此存储过程? 我正在使用Sql Server 2005. 我想向存储过程添加两个参数。 PageIndex和PageSize。 请帮助。添加自定义分页到SQL查询

USE [tadarokatbase] 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[aspnet_Membership_Find] 
(
    @SearchUsingOR bit = null , 

    @ApplicationId uniqueidentifier = null , 

    @UserId uniqueidentifier = null , 

    @Password nvarchar (128) = null , 

    @PasswordFormat int = null , 

    @PasswordSalt nvarchar (128) = null , 

    @MobilePin nvarchar (16) = null , 

    @Email nvarchar (256) = null , 

    @LoweredEmail nvarchar (256) = null , 

    @PasswordQuestion nvarchar (256) = null , 

    @PasswordAnswer nvarchar (128) = null , 

    @IsApproved bit = null , 

    @IsLockedOut bit = null , 

    @CreateDate datetime = null , 

    @LastLoginDate datetime = null , 

    @LastPasswordChangedDate datetime = null , 

    @LastLockoutDate datetime = null , 

    @FailedPasswordAttemptCount int = null , 

    @FailedPasswordAttemptWindowStart datetime = null , 

    @FailedPasswordAnswerAttemptCount int = null , 

    @FailedPasswordAnswerAttemptWindowStart datetime = null , 

    @Comment ntext = null 
) 
AS 



    IF ISNULL(@SearchUsingOR, 0) <> 1 
    BEGIN 
    SELECT 
     [ApplicationId] 
    , [UserId] 
    , [Password] 
    , [PasswordFormat] 
    , [PasswordSalt] 
    , [MobilePIN] 
    , [Email] 
    , [LoweredEmail] 
    , [PasswordQuestion] 
    , [PasswordAnswer] 
    , [IsApproved] 
    , [IsLockedOut] 
    , [CreateDate] 
    , [LastLoginDate] 
    , [LastPasswordChangedDate] 
    , [LastLockoutDate] 
    , [FailedPasswordAttemptCount] 
    , [FailedPasswordAttemptWindowStart] 
    , [FailedPasswordAnswerAttemptCount] 
    , [FailedPasswordAnswerAttemptWindowStart] 
    , [Comment] 
    FROM 
    [dbo].[aspnet_Membership] 
    WHERE 
    ([ApplicationId] = @ApplicationId OR @ApplicationId IS NULL) 
    AND ([UserId] = @UserId OR @UserId IS NULL) 
    AND ([Password] = @Password OR @Password IS NULL) 
    AND ([PasswordFormat] = @PasswordFormat OR @PasswordFormat IS NULL) 
    AND ([PasswordSalt] = @PasswordSalt OR @PasswordSalt IS NULL) 
    AND ([MobilePIN] = @MobilePin OR @MobilePin IS NULL) 
    AND ([Email] = @Email OR @Email IS NULL) 
    AND ([LoweredEmail] = @LoweredEmail OR @LoweredEmail IS NULL) 
    AND ([PasswordQuestion] = @PasswordQuestion OR @PasswordQuestion IS NULL) 
    AND ([PasswordAnswer] = @PasswordAnswer OR @PasswordAnswer IS NULL) 
    AND ([IsApproved] = @IsApproved OR @IsApproved IS NULL) 
    AND ([IsLockedOut] = @IsLockedOut OR @IsLockedOut IS NULL) 
    AND ([CreateDate] = @CreateDate OR @CreateDate IS NULL) 
    AND ([LastLoginDate] = @LastLoginDate OR @LastLoginDate IS NULL) 
    AND ([LastPasswordChangedDate] = @LastPasswordChangedDate OR @LastPasswordChangedDate IS NULL) 
    AND ([LastLockoutDate] = @LastLockoutDate OR @LastLockoutDate IS NULL) 
    AND ([FailedPasswordAttemptCount] = @FailedPasswordAttemptCount OR @FailedPasswordAttemptCount IS NULL) 
    AND ([FailedPasswordAttemptWindowStart] = @FailedPasswordAttemptWindowStart OR @FailedPasswordAttemptWindowStart IS NULL) 
    AND ([FailedPasswordAnswerAttemptCount] = @FailedPasswordAnswerAttemptCount OR @FailedPasswordAnswerAttemptCount IS NULL) 
    AND ([FailedPasswordAnswerAttemptWindowStart] = @FailedPasswordAnswerAttemptWindowStart OR @FailedPasswordAnswerAttemptWindowStart IS NULL) 

    END 
    ELSE 
    BEGIN 
    SELECT 
     [ApplicationId] 
    , [UserId] 
    , [Password] 
    , [PasswordFormat] 
    , [PasswordSalt] 
    , [MobilePIN] 
    , [Email] 
    , [LoweredEmail] 
    , [PasswordQuestion] 
    , [PasswordAnswer] 
    , [IsApproved] 
    , [IsLockedOut] 
    , [CreateDate] 
    , [LastLoginDate] 
    , [LastPasswordChangedDate] 
    , [LastLockoutDate] 
    , [FailedPasswordAttemptCount] 
    , [FailedPasswordAttemptWindowStart] 
    , [FailedPasswordAnswerAttemptCount] 
    , [FailedPasswordAnswerAttemptWindowStart] 
    , [Comment] 
    FROM 
    [dbo].[aspnet_Membership] 
    WHERE 
    ([ApplicationId] = @ApplicationId AND @ApplicationId is not null) 
    OR ([UserId] = @UserId AND @UserId is not null) 
    OR ([Password] = @Password AND @Password is not null) 
    OR ([PasswordFormat] = @PasswordFormat AND @PasswordFormat is not null) 
    OR ([PasswordSalt] = @PasswordSalt AND @PasswordSalt is not null) 
    OR ([MobilePIN] = @MobilePin AND @MobilePin is not null) 
    OR ([Email] = @Email AND @Email is not null) 
    OR ([LoweredEmail] = @LoweredEmail AND @LoweredEmail is not null) 
    OR ([PasswordQuestion] = @PasswordQuestion AND @PasswordQuestion is not null) 
    OR ([PasswordAnswer] = @PasswordAnswer AND @PasswordAnswer is not null) 
    OR ([IsApproved] = @IsApproved AND @IsApproved is not null) 
    OR ([IsLockedOut] = @IsLockedOut AND @IsLockedOut is not null) 
    OR ([CreateDate] = @CreateDate AND @CreateDate is not null) 
    OR ([LastLoginDate] = @LastLoginDate AND @LastLoginDate is not null) 
    OR ([LastPasswordChangedDate] = @LastPasswordChangedDate AND @LastPasswordChangedDate is not null) 
    OR ([LastLockoutDate] = @LastLockoutDate AND @LastLockoutDate is not null) 
    OR ([FailedPasswordAttemptCount] = @FailedPasswordAttemptCount AND @FailedPasswordAttemptCount is not null) 
    OR ([FailedPasswordAttemptWindowStart] = @FailedPasswordAttemptWindowStart AND @FailedPasswordAttemptWindowStart is not null) 
    OR ([FailedPasswordAnswerAttemptCount] = @FailedPasswordAnswerAttemptCount AND @FailedPasswordAnswerAttemptCount is not null) 
    OR ([FailedPasswordAnswerAttemptWindowStart] = @FailedPasswordAnswerAttemptWindowStart AND @FailedPasswordAnswerAttemptWindowStart is not null) 
    SELECT @@ROWCOUNT   
    END 
+1

你谷歌? [自定义分页](http://www.codeproject.com/KB/database/CustomPagingStoredProc.aspx),[高效分页大量数据](http://msdn.microsoft.com/zh-cn/library/ bb445504.aspx)... – 2011-04-12 10:38:02

+0

好的,谢谢。我尝试添加自定义分页到我的SP。 – Shahin 2011-04-12 10:44:20

回答

3

这可能会帮助:

USE [tadarokatbase] 
GO 

SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[aspnet_Membership_Find] 
(
    @SearchUsingOR bit = null , 
    @ApplicationId uniqueidentifier = null , 
    @UserId uniqueidentifier = null , 
    @Password nvarchar (128) = null , 
    @PasswordFormat int = null , 
    @PasswordSalt nvarchar (128) = null , 
    @MobilePin nvarchar (16) = null , 
    @Email nvarchar (256) = null , 
    @LoweredEmail nvarchar (256) = null , 
    @PasswordQuestion nvarchar (256) = null , 
    @PasswordAnswer nvarchar (128) = null , 
    @IsApproved bit = null , 
    @IsLockedOut bit = null , 
    @CreateDate datetime = null , 
    @LastLoginDate datetime = null , 
    @LastPasswordChangedDate datetime = null , 
    @LastLockoutDate datetime = null , 
    @FailedPasswordAttemptCount int = null , 
    @FailedPasswordAttemptWindowStart datetime = null , 
    @FailedPasswordAnswerAttemptCount int = null , 
    @FailedPasswordAnswerAttemptWindowStart datetime = null , 
    @Comment ntext = null , 


    @StartRowIndex  int = null, 
    @PageSize   int = null 

) 

AS 

    Declare @UpperBand int 
    Declare @LowerBand int 


    IF ISNULL(@SearchUsingOR, 0) <> 1 
    BEGIN 

    SET @LowerBand = @startRowIndex 
    SET @UpperBand = @startRowIndex + @pageSize 

    WITH tempPagedEntities AS 
      (
    SELECT 
     [ApplicationId] 
    , [UserId] 
    , [Password] 
    , [PasswordFormat] 
    , [PasswordSalt] 
    , [MobilePIN] 
    , [Email] 
    , [LoweredEmail] 
    , [PasswordQuestion] 
    , [PasswordAnswer] 
    , [IsApproved] 
    , [IsLockedOut] 
    , [CreateDate] 
    , [LastLoginDate] 
    , [LastPasswordChangedDate] 
    , [LastLockoutDate] 
    , [FailedPasswordAttemptCount] 
    , [FailedPasswordAttemptWindowStart] 
    , [FailedPasswordAnswerAttemptCount] 
    , [FailedPasswordAnswerAttemptWindowStart] 
    , [Comment] 

    ,ROW_NUMBER() OVER (ORDER BY UserId asc) as RowNumber 

    FROM 
    [dbo].[aspnet_Membership] 
    WHERE 
    ([ApplicationId] = @ApplicationId OR @ApplicationId IS NULL) 
    AND ([UserId] = @UserId OR @UserId IS NULL) 
    AND ([Password] = @Password OR @Password IS NULL) 
    AND ([PasswordFormat] = @PasswordFormat OR @PasswordFormat IS NULL) 
    AND ([PasswordSalt] = @PasswordSalt OR @PasswordSalt IS NULL) 
    AND ([MobilePIN] = @MobilePin OR @MobilePin IS NULL) 
    AND ([Email] = @Email OR @Email IS NULL) 
    AND ([LoweredEmail] = @LoweredEmail OR @LoweredEmail IS NULL) 
    AND ([PasswordQuestion] = @PasswordQuestion OR @PasswordQuestion IS NULL) 
    AND ([PasswordAnswer] = @PasswordAnswer OR @PasswordAnswer IS NULL) 
    AND ([IsApproved] = @IsApproved OR @IsApproved IS NULL) 
    AND ([IsLockedOut] = @IsLockedOut OR @IsLockedOut IS NULL) 
    AND ([CreateDate] = @CreateDate OR @CreateDate IS NULL) 
    AND ([LastLoginDate] = @LastLoginDate OR @LastLoginDate IS NULL) 
    AND ([LastPasswordChangedDate] = @LastPasswordChangedDate OR @LastPasswordChangedDate IS NULL) 
    AND ([LastLockoutDate] = @LastLockoutDate OR @LastLockoutDate IS NULL) 
    AND ([FailedPasswordAttemptCount] = @FailedPasswordAttemptCount OR @FailedPasswordAttemptCount IS NULL) 
    AND ([FailedPasswordAttemptWindowStart] = @FailedPasswordAttemptWindowStart OR @FailedPasswordAttemptWindowStart IS NULL) 
    AND ([FailedPasswordAnswerAttemptCount] = @FailedPasswordAnswerAttemptCount OR @FailedPasswordAnswerAttemptCount IS NULL) 
    AND ([FailedPasswordAnswerAttemptWindowStart] = @FailedPasswordAnswerAttemptWindowStart OR @FailedPasswordAnswerAttemptWindowStart IS NULL) 

    ) 
      SELECT 
       * 
      FROM 
       tempPagedEntities 
      WHERE 
       RowNumber > @LowerBand AND RowNumber <= @UpperBand 

    END 
    ELSE 
    BEGIN 

    BEGIN 
      WITH tempPagedEntities AS 
      (
    SELECT 
     [ApplicationId] 
    , [UserId] 
    , [Password] 
    , [PasswordFormat] 
    , [PasswordSalt] 
    , [MobilePIN] 
    , [Email] 
    , [LoweredEmail] 
    , [PasswordQuestion] 
    , [PasswordAnswer] 
    , [IsApproved] 
    , [IsLockedOut] 
    , [CreateDate] 
    , [LastLoginDate] 
    , [LastPasswordChangedDate] 
    , [LastLockoutDate] 
    , [FailedPasswordAttemptCount] 
    , [FailedPasswordAttemptWindowStart] 
    , [FailedPasswordAnswerAttemptCount] 
    , [FailedPasswordAnswerAttemptWindowStart] 
    , [Comment] 

    ,ROW_NUMBER() OVER (ORDER BY UserId asc) as RowNumber 

    FROM 
    [dbo].[aspnet_Membership] 
    WHERE 
    ([ApplicationId] = @ApplicationId AND @ApplicationId is not null) 
    OR ([UserId] = @UserId AND @UserId is not null) 
    OR ([Password] = @Password AND @Password is not null) 
    OR ([PasswordFormat] = @PasswordFormat AND @PasswordFormat is not null) 
    OR ([PasswordSalt] = @PasswordSalt AND @PasswordSalt is not null) 
    OR ([MobilePIN] = @MobilePin AND @MobilePin is not null) 
    OR ([Email] = @Email AND @Email is not null) 
    OR ([LoweredEmail] = @LoweredEmail AND @LoweredEmail is not null) 
    OR ([PasswordQuestion] = @PasswordQuestion AND @PasswordQuestion is not null) 
    OR ([PasswordAnswer] = @PasswordAnswer AND @PasswordAnswer is not null) 
    OR ([IsApproved] = @IsApproved AND @IsApproved is not null) 
    OR ([IsLockedOut] = @IsLockedOut AND @IsLockedOut is not null) 
    OR ([CreateDate] = @CreateDate AND @CreateDate is not null) 
    OR ([LastLoginDate] = @LastLoginDate AND @LastLoginDate is not null) 
    OR ([LastPasswordChangedDate] = @LastPasswordChangedDate AND @LastPasswordChangedDate is not null) 
    OR ([LastLockoutDate] = @LastLockoutDate AND @LastLockoutDate is not null) 
    OR ([FailedPasswordAttemptCount] = @FailedPasswordAttemptCount AND @FailedPasswordAttemptCount is not null) 
    OR ([FailedPasswordAttemptWindowStart] = @FailedPasswordAttemptWindowStart AND @FailedPasswordAttemptWindowStart is not null) 
    OR ([FailedPasswordAnswerAttemptCount] = @FailedPasswordAnswerAttemptCount AND @FailedPasswordAnswerAttemptCount is not null) 
    OR ([FailedPasswordAnswerAttemptWindowStart] = @FailedPasswordAnswerAttemptWindowStart AND @FailedPasswordAnswerAttemptWindowStart is not null) 

    ) 
      SELECT 
       * 
      FROM 
       tempPagedEntities 
      WHERE 
       RowNumber > @LowerBand AND RowNumber <= @UpperBand 

    SELECT @@ROWCOUNT   
    END 

    END 
+0

谢谢,但我不能执行SP,这个错误返回: 消息319,级别15,状态1,过程aspnet_Membership_Find,行44 关键字'与'附近的语法不正确。如果此语句是公用表表达式或xmlnamespaces子句,则前面的语句必须以分号结尾。 – Shahin 2011-04-12 12:55:46

+0

此命令SELECT @@ ROWCOUNT只返回值等于页面大小 – nvtthang 2013-05-30 07:40:26