sp_send_dbmail卡在循环中发送数百封电子邮件

时间:2015-06-04 15:37:34

标签: sql-server-2008 sp-send-dbmail

我设置了一个触发器以插入到表中,然后希望安排一个代理作业从该表发送电子邮件。我有触发器工作,我认为我有SP发送电子邮件工作正常但我的循环卡住并发送数百封电子邮件,然后取消SP。对我做错了什么的想法?我使用BatchEmailID作为标记来了解需要发送的内容以及不在哪里' 0' =它还没有发送,需要去' 1' =它已被发送,所以忽略。

创建表:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tb_BatchEmail](
    [BatchEmailID] [bit] NULL,
    [To] [varchar](50) NULL,
    [Body] [varchar](255) NULL,
    [Subject] [varchar](20) NULL,
    [Profile] [varchar](50) NULL,
    [OrderID] [varchar](25) NULL,
    [OrderDateTime] [datetime] NULL,
    [SentDateTime] [datetime] NULL
) ON [PRIMARY]

GO

插入值:

    INSERT INTO tb_BatchEmail
VALUES (
    '0'
    ,'someemail@address.com'
    ,'msg body'
    ,'Test Subject'
    ,'dbmail profile'
    ,'123456.001'
    ,'6/4/2015'
    ,NULL
    ),
    (
    '0'
    ,'someemail@address.com'
    ,'msg body'
    ,'Test Subject'
    ,'dbmail profile'
    ,'123456.002'
    ,'6/4/2015'
    ,NULL
    )

sp_send_dbmail:

while 1=1

begin

declare @BatchEmailID bit
declare @To varchar (25)
declare @Body varchar (250)
declare @Subject varchar (25)
declare @ProfileName varchar (20)

    select top 1 
           @BatchEmailID = BatchEmailID,
           @To = [To],
           @Body = Body,
           @Subject = [Subject],
           @ProfileName = [Profile]
    from tb_BatchEmail
    where BatchEmailID = 0

    if(@BatchEmailID is null)
    break;

    EXEC msdb.dbo.sp_send_dbmail @recipients = @To
            ,@body = @Body
            ,@subject = @Subject
            ,@profile_name = @ProfileName

  update tb_BatchEmail
    set BatchEmailID = 1, SentDateTime = GETDATE()
    where BatchEmailID = @BatchEmailID

end

1 个答案:

答案 0 :(得分:0)

首先关闭,要更新表格,每行需要一个唯一值。

按如下方式更改您的表格:

CREATE TABLE [dbo].[tb_BatchEmail](
    [Id] int identity(1,1),
    [BatchEmailID] [bit] NULL,
    [To] [varchar](50) NULL,
    [Body] [varchar](255) NULL,
    [Subject] [varchar](20) NULL,
    [Profile] [varchar](50) NULL,
    [OrderID] [varchar](25) NULL,
    [OrderDateTime] [datetime] NULL,
    [SentDateTime] [datetime] NULL
) ON [PRIMARY]

GO

新[Id]列将用于引用相关行。

您的循环变为此(使用测试表),只需注释掉我的选择,并取消注释电子邮件位:

declare @tb_BatchEmail table (Id int identity(1,1),
                              BatchEmailID bit,
                              [To] varchar(25),
                              Body varchar(250),
                              [Subject] varchar(25),
                              [Profile] varchar(20),
                              SentDateTime datetime)
insert into @tb_BatchEmail
select 0,'joe@domain.com','just a test','test','myprofile',null

declare @Id int
declare @To varchar (25)
declare @Body varchar (250)
declare @Subject varchar (25)
declare @ProfileName varchar (20)

while (select count(*) from @tb_BatchEmail where BatchEmailID=0) > 0
begin
    select top 1 
            @Id = Id,
            @To = [To],
            @Body = Body,
            @Subject = [Subject],
            @ProfileName = [Profile]
    from @tb_BatchEmail
    where BatchEmailID = 0

    --EXEC msdb.dbo.sp_send_dbmail @recipients = @To
    --      ,@body = @Body
    --      ,@subject = @Subject
    --      ,@profile_name = @ProfileName

    select @Id, @To, @Body, @Subject, @ProfileName

    update @tb_BatchEmail
    set BatchEmailID = 1, SentDateTime = GETDATE()
    where Id = @Id
end