自动SQL电子邮件结果不佳

时间:2015-06-29 20:24:48

标签: sql-server sql-server-2008 email stored-procedures

我已经创建了一个自动发送的电子邮件,告诉我的客户他们订购的产品是否已经订购。它使用一个SQL视图和两个存储过程,如下所示。我的问题是,当我执行它时,第一个延期交货发送一个标题为“sql server message”的电子邮件,并且没有sql结果。所有后续延期交货均正确运行,并且正文中包含正确的主题和数据。我的sp中有没有做错的事情?

此sp首先运行,并构建一个包含延期交货零件的客户列表。

USE [001]
GO
/****** Object:  StoredProcedure [mfg].[sp_BackorderEmailListing]    Script Date: 6/29/2015 4:09:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Nicholas Dininno>
-- Create date: <6/15/15>
-- Description: <Runs through a distinct list of Franchisees that have 
-- orders on backorder, and passes their info to [mfg].[sp_SendBackorderEmail]>
-- =============================================
ALTER PROCEDURE [mfg].[sp_BackorderEmailListing]
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

Declare @cmp_code nvarchar(50)
Declare @email nvarchar(Max)  
Declare cur_BackOrdSP Cursor

For SELECT DISTINCT  [Franchise ID]  FROM [001].[mfg].[Backorder_Report]

Open cur_BackOrdSP

Fetch Next From cur_BackOrdSP

While @@FETCH_STATUS = 0

Begin
    --find email address
    SELECT @email = [cmp_e_mail] FROM [001].[mfg].[Backorder_Report] where [Franchise ID]=@cmp_code

    execute [mfg].[sp_SendBackorderEmail] @cmp_code, @email

    Fetch Next From cur_BackOrdSP Into @cmp_code
END

Close cur_BackOrdSP
Deallocate cur_BackOrdSP
END

下一部分是构建电子邮件的地方。

USE [001]
GO
/****** Object:  StoredProcedure [mfg].[sp_SendBackorderEmail]    Script Date: 6/29/2015 4:12:51 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Nick Dininno
-- Create date: 6/25/2015
-- Description: Sends email to Franchisees with backordered parts
-- =============================================
ALTER PROCEDURE [mfg].[sp_SendBackorderEmail]
    -- Add the parameters for the stored procedure here
(@cmp_code nvarchar(5), @email nvarchar(50))
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

DECLARE @profile nvarchar(50)
DECLARE @subject nvarchar(100)
DECLARE @querystr nvarchar (MAX)


set @profile = 'Reports'
set @subject  =  'Backorder Report for ' + @cmp_code
set @querystr = 'SET NOCOUNT ON
SELECT  [Order #],[Order Date],[Item #],[Description]
  FROM [001].[mfg].[Backorder_Report]
  WHERE [Franchise ID] = '''+@cmp_code+'''';


EXEC msdb.dbo.sp_send_dbmail
@profile_name = @profile,
@recipients = 'me@company.com',
@subject = @subject,
@body = 'Note: This is an automatic e-mail message generated by the Parts Department.  

Our records indicate that you ordered the parts shown below.  Unfortunately these parts are currently on backorder.  They will be shipped to you as soon as they become available.  In the meantime, if you have any questions or concerns regarding this order please contact the Parts Department via email parts@lawndoctor.com or call 732-308-2300 x4.

Thank you!
Parts Department
parts@company.com
', 
@query = @querystr

END

收件人me@company.com将在我确定它与@email参数一起使用后被替换

1 个答案:

答案 0 :(得分:0)

我的问题是语法问题。在第一个程序中,我没有将特许经营权ID传递给@cmp_code。

Fetch Next From cur_BackOrdSP

应该是

Fetch Next From cur_BackOrdSP into @cmp_code