Добавить пользовательский пейджинг в SQL-запрос - PullRequest
0 голосов
/ 12 апреля 2011

У меня есть хранимая процедура, которая извлекает записи из таблицы.Я новичок в написании хранимых процедур.Как я могу добавить пейджинг к этой хранимой процедуре?Я использую Sql Server 2005. Я хочу добавить еще два аргумента в хранимую процедуру.PageIndex и PageSize.Пожалуйста, помогите.

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 Ответ

3 голосов
/ 12 апреля 2011

Это может помочь:

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
...