SQL Server存储过程以发送电子邮件

前端之家收集整理的这篇文章主要介绍了SQL Server存储过程以发送电子邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我第一次尝试编写通过电子邮件发送给某人的存储过程.尝试执行时,我收到以下错误
Msg 102,Level 15,State 1,Procedure EmailTodaysErrors,Line 14
Incorrect Syntax near '@MailServer'.
Msg 137,State 2,Line 26
Must declare the scalar variable "@mailserver".
Msg 137,Line 33
Must declare the scalar variable "@Body".

我正在使用的代码是:

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','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

任何帮助将不胜感激.提前致谢.

解决方法

在@body行之后你丢失了一个逗号,这就是抛弃你的声明.

在这里添加

@Body varchar(8000),-- HERE
@MailServer varchar(100) = 'localhost'
原文链接:https://www.f2er.com/mssql/79585.html

猜你在找的MsSQL相关文章