aspnet_Membership_FindUsersByEmail

ALTER  PROCEDURE dbo.aspnet_Membership_FindUsersByEmail
    
@ApplicationName       NVARCHAR(256),
    
@EmailToMatch          NVARCHAR(256),
    
@PageIndex             INT,
    
@PageSize              INT
AS
BEGIN
    
DECLARE @ApplicationId UNIQUEIDENTIFIER
    
SELECT  @ApplicationId = NULL
    
SELECT  @ApplicationId = ApplicationId FROM dbo.aspnet_Applications WHERE LOWER(@ApplicationName= LoweredApplicationName
    
IF (@ApplicationId IS NULL)
        
RETURN 0

    
-- Set the page bounds
    DECLARE @PageLowerBound INT ---开始显示的记录
    DECLARE @PageUpperBound INT ---结束显示的记录(用下面的算就知道了)
    DECLARE @TotalRecords   INT ---总记录数
    SET @PageLowerBound = @PageSize * @PageIndex
    
SET @PageUpperBound = @PageSize - 1 + @PageLowerBound

    
-- Create a temp table TO store the select results
    --创建一个临时表存储查询出的结果
    CREATE TABLE #PageIndexForUsers
    (
        IndexId 
int IDENTITY (01NOT NULL,
        UserId 
UNIQUEIDENTIFIER
    )

    
-- Insert into our temp table
    --为临时表插数据
    IF@EmailToMatch IS NULL )
        
INSERT INTO #PageIndexForUsers (UserId)
            
SELECT u.UserId
            
FROM   dbo.aspnet_Users u, dbo.aspnet_Membership m
            
WHERE  u.ApplicationId = @ApplicationId AND m.UserId = u.UserId AND m.Email IS NULL
            
ORDER BY m.LoweredEmail
    
--如果邮件地址为空,则选出所有用户放如临时表
    ELSE
        
INSERT INTO #PageIndexForUsers (UserId)
            
SELECT u.UserId
            
FROM   dbo.aspnet_Users u, dbo.aspnet_Membership m
            
WHERE  u.ApplicationId = @ApplicationId AND m.UserId = u.UserId AND m.LoweredEmail LIKE LOWER(@EmailToMatch)
            
ORDER BY m.LoweredEmail
    
--如果不为空,则查询具有相似地址的用户放入临时表
    SELECT  u.UserName, m.Email, m.PasswordQuestion, m.Comment, m.IsApproved,
            m.CreateDate,
            m.LastLoginDate,
            u.LastActivityDate,
            m.LastPasswordChangedDate,
            u.UserId, m.IsLockedOut,
            m.LastLockoutDate
    
FROM   dbo.aspnet_Membership m, dbo.aspnet_Users u, #PageIndexForUsers p
    
WHERE  u.UserId = p.UserId AND u.UserId = m.UserId AND
           p.IndexId 
>= @PageLowerBound AND p.IndexId <= @PageUpperBound
    
ORDER BY m.LoweredEmail
    
/*三表连接通过USERID,临时表的自动增长列主要用来控制显示数的.这个好象用于分页.
        若不考虑分页,可不用临时表.
*/

    
SELECT  @TotalRecords = COUNT(*--查询总记录数
    FROM    #PageIndexForUsers
    
RETURN @TotalRecords
END
原文地址:https://www.cnblogs.com/ruanbl/p/490046.html