2011-08-18 93 views
8

這是我第一次嘗試編寫向某人發送電子郵件的存儲過程。當試圖執行我得到這些錯誤:SQL Server存儲過程發送電子郵件

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

CREATE PROCEDURE EmailTodaysErrors 
@SenderName varchar(100), 
@SenderAddress varchar(100), 
@RecipientName varchar(100), 
@RecipientAddress varchar(100), 
@Subject varchar(200), 
@Body varchar(8000) 
@MailServer varchar(100) = 'localhost' 
AS 
SET NOCOUNT ON; 
declare @oMail int --Object reference 
declare @resultcode int 
EXEC @resultcode = sp_OACreate 'CDONTS.NewMail', @oMail OUT 

if @resultcode = 0 
BEGIN 
EXEC @resultcode = sp_OASetProperty @oMail, 'RemoteHost', @mailserver 
EXEC @resultcode = sp_OASetProperty @oMail, 'FromName', @SenderName 
EXEC @resultcode = sp_OASetProperty @oMail, 'FromAddress', @SenderAddress 
EXEC @resultcode = sp_OASetProperty @oMail, 'From', @SenderAddress 
EXEC @resultcode = sp_OASetProperty @oMail, 'To', @RecipientAddress 
EXEC @resultcode = sp_OASetProperty @oMail, 'Subject', @Subject 
EXEC @resultcode = sp_OASetProperty @oMail, 'Body', @Body 
EXEC @resultcode = sp_OAMethod @oMail, 'Send', NULL 
EXEC sp_OADestroy @oMail 
END 

set nocount off 
GO 

任何幫助將不勝感激:我使用這是造成這是

Msg 102, Level 15, State 1, Procedure EmailTodaysErrors, Line 14 
Incorrect syntax near '@MailServer'. 
Msg 137, Level 15, State 2, Procedure EmailTodaysErrors, Line 26 
Must declare the scalar variable "@mailserver". 
Msg 137, Level 15, State 2, Procedure EmailTodaysErrors, Line 33 
Must declare the scalar variable "@Body". 

的代碼。提前致謝。

回答

11

您在@body行之後缺少一個逗號,該行會拋出您的聲明。

添加在這裏:

@Body varchar(8000), -- HERE 
@MailServer varchar(100) = 'localhost' 
+0

謝謝這個作品 –

+0

高興地幫助。總是檢查你的逗號和括號! – JNK

11

不要使用sp_oa_family發送郵件,已經有一個SQL Server的內置解決方案:Database Mail。只需在服務器上正確配置數據庫郵件,然後致電sp_send_dbmail

+0

我試過這個方法很好,但我碰到困難的時候,它說配置文件名稱是無效 –

+0

正如我說,*配置*數據庫郵件第一:http://technet.microsoft.com/en-us/library/ms175951.aspx –

2

有一個逗號在參數丟失:

@Body varchar(8000), ---- HERE 
@MailServer varchar(100) = 'localhost' 
相關問題