如何修改存储过程以便它可以根据请求模式处理请求?

时间:2013-09-09 03:43:13

标签: sql sql-server stored-procedures

我们编写了一个存储过程,该过程使用SQL Server的send_dbmail功能向潜在用户发送电子邮件,并附上注册详情。

此存储过程效果很好。

我们目前唯一的问题是用户还希望索取他们忘记密码的副本。

我可以编写另一个存储过程来处理这个问题吗?但是,有没有办法修改下面的存储过程,以便它识别请求的模式?

例如,目前用户创建了一个帐户,帐户的详细信息存储在tblLogin表中。

此存储过程会抓取帐户信息,将其存储在通知表中,最后将此帐户的详细信息发送给刚刚注册的用户。

同样,我们希望在用户使用Forgotten Password功能来请求密码时处理相同的存储过程。

如何根据所请求的内容修改存储过程发送电子邮件?

换句话说,如果tblLogin表中存储的数据用于new account创建,则存储过程应该抓取它并向用户发送包含详细信息的电子邮件。

如果数据是forgotten password,则存储过程也应仅通过电子邮件发送该信息。

这可以使用下面的存储过程吗?

ALTER PROCEDURE [dbo].[GetRegistrationInfo] 
AS
BEGIN
    DECLARE Register_Cursor CURSOR FOR 
        SELECT 
            LoginId, FullName, email, Password
        FROM 
            [tblLogin]
        WHERE 
            ProcessedFlag = 'No'
        ORDER BY 
            LoginId DESC

    OPEN Register_Cursor

    DECLARE @LoginId INT
    DECLARE @fullname NVARCHAR(100)
    DECLARE @email NVARCHAR(MAX)
    DECLARE @password NVARCHAR(20)

    -- Get the current MAX ID 
    DECLARE @mailID as INT 

    -- Start reading each record from the cursor. 
    FETCH Register_Cursor INTO @LoginId, @fullname, @email, @password

    WHILE @@FETCH_STATUS = 0 
    BEGIN 
        --set @mailID = (SELECT max(mailID) from Notifications) Not needed; let's auto-genereate the id
        INSERT INTO [Notifications] (mailContent, LoginId, FullName, email, Password, sender, Sent) 
        VALUES ('This is a computer generated email message. 
Please DO NOT use the REPLY button above to respond to this email. 

Dear '+@FullName+': 

Thanks for registering to take the Training!

Below are details of your registration information:

Your UserName is: '+@email+'. 

Your Password is: '+@password+'. 

Once you have retrieved your login information, please click the link below to get back to Training login screen and begin to begin to enjoy the benefits of membership. 


http://servername/training/

Regards, 
The Registrations & Elections Office.', @LoginId, @FullName, @email, @Password, 'NoReply@serverdomain', 'No') 

        FETCH Register_Cursor INTO @LoginId, @FullName, @email, @password
    END

    CLOSE Register_Cursor  
    DEALLOCATE Register_Cursor
END

BEGIN
    DECLARE MAIL_CURSOR CURSOR FOR
        SELECT mailid, sender, mailcontent
        FROM [Notifications]
        WHERE Sent = 'No'

    DECLARE @mail1 INT
    DECLARE @sender NVARCHAR(100)
    DECLARE @content1 NVARCHAR(4000) 

    OPEN MAIL_CURSOR

    FETCH MAIL_CURSOR INTO @mail1, @sender, @content1

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SELECT @email = @email + ';' + Email
        FROM [Notifications]
        WHERE sent = 'No'

        -- exec sp_send_cdontsmail @mail1, null,null,@content1,null
        EXEC msdb.dbo.sp_send_dbmail
                  @profile_name = 'The Office', 
                  @recipients = @email, -- your email
                  --@blind_copy_recipients = @email,
                  @subject = 'Your Account Details',
                  @body = @content1;

        -- Update the record in Notifications table where Sent = 'No'.
        UPDATE [Notifications] 
        SET Sent = 'Yes' 
        WHERE Sent = 'No' AND mailid = @mail1

        UPDATE [tblLogin] 
        SET ProcessedFlag = 'Yes' 
        WHERE ProcessedFlag = 'No' AND LoginId = @LoginId 

        FETCH MAIL_CURSOR INTO @mail1, @sender, @content1
    END

    CLOSE MAIL_CURSOR
    DEALLOCATE MAIL_CURSOR
END

1 个答案:

答案 0 :(得分:0)

很抱歉,如果我在这里遗漏了一些内容,但看起来你只需要一个IF-ELSE语句,它将在一个案例中发送一条消息,另一条消息则另外发送消息......