2011-04-12 79 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