Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何为要通过电子邮件发送作为输出的查询分配参数_Sql_Sql Server_Stored Procedures_Sql Server 2000 - Fatal编程技术网

Sql 如何为要通过电子邮件发送作为输出的查询分配参数

Sql 如何为要通过电子邮件发送作为输出的查询分配参数,sql,sql-server,stored-procedures,sql-server-2000,Sql,Sql Server,Stored Procedures,Sql Server 2000,我正在尝试将查询结果作为电子邮件正文发送电子邮件。。。我想我已经很接近了。。。我决定使用两个存储过程来实现这一点(第一个存储过程是一个示例电子邮件代码,因为它是SQL Server 2000,它没有像2005和2008那样的内置发送邮件功能 以下是我到目前为止对存储过程所做的操作……正如您所看到的,我声明@Body。然后我将@Body分配给运行查询的存储过程getpaidout1929(Exec@Body=sp_getpaidout1929): SELECT Store_Id, P

我正在尝试将查询结果作为电子邮件正文发送电子邮件。。。我想我已经很接近了。。。我决定使用两个存储过程来实现这一点(第一个存储过程是一个示例电子邮件代码,因为它是SQL Server 2000,它没有像2005和2008那样的内置发送邮件功能

以下是我到目前为止对存储过程所做的操作……正如您所看到的,我声明@Body。然后我将@Body分配给运行查询的存储过程getpaidout1929(
Exec@Body=sp_getpaidout1929
):

SELECT        Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName
FROM            Paid_Out_Tb
WHERE        (Store_Id = 1929) AND (Paid_Out_Amount > 50) AND (Paid_Out_Datetime BETWEEN CONVERT(DATETIME, '2012-06-01 00:00:00', 102) AND CONVERT(DATETIME, 
                         '2012-06-30 00:00:00', 102)) 
然后我执行存储过程

Exec sp_SQLNotify ‘to@who.com', ‘who@who.com, ‘test’
它向我发送电子邮件,但正文消息为“0”

AJ

创建过程[dbo]。[sp_SQLNotify]
@来自varchar(100),
@至varchar(100),
@主题varchar(100)=“”
/*********************************************************************
此存储过程接受上述参数并发送电子邮件。
所有邮件配置都在存储过程中硬编码。
必要时将注释添加到存储过程中。
以下MSDN网站提供了对CDOSYS对象的引用:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/\u cdosys\u messaging.asp
***********************************************************************/ 
作为
声明@iMsg int
声明@hr int
声明@source varchar(255)
声明@description varchar(500)
声明@output varchar(1000)
声明@Body Varchar(8000)
--*************创建CDO.Message对象************************
EXEC@hr=sp_OACreate'CDO.Message',@iMsg OUT
--***************配置消息对象******************
--这是为了配置远程SMTP服务器。
-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/\u cdosys\u schema\u configuration\u sendusing.asp
EXEC@hr=sp_OASetProperty@iMsg,'Configuration.fields('http://schemas.microsoft.com/cdo/configuration/sendusing)。值','2'
--这是为了配置服务器名称或IP地址。
--用SMTP服务器的名称或IP替换MailServerName。
EXEC@hr=sp_OASetProperty@iMsg,'Configuration.fields('http://schemas.microsoft.com/cdo/configuration/smtpserver“”。值“,”10.0.1.3“
--将配置保存到消息对象。
EXEC@hr=sp_OAMethod@iMsg,'Configuration.Fields.Update',null
Exec@Body=sp_getpaidout1929
--设置电子邮件参数。
EXEC@hr=sp_OASetProperty@iMsg,'到',@到
EXEC@hr=sp_OASetProperty@iMsg,“From”,“From”
EXEC@hr=sp_OASetProperty@iMsg,'Subject',@Subject
--如果您使用的是HTML电子邮件,请使用“HTMLBody”而不是“TextBody”。
EXEC@hr=sp_OASetProperty@iMsg,'TextBody',@Body
EXEC@hr=sp_OAMethod@iMsg,'Send',NULL
--样本错误处理。
如果@hr 0
选择@hr
开始
EXEC@hr=sp_OAGetErrorInfo NULL、@source OUT、@description OUT
如果@hr=0
开始
选择@output='Source:'+@Source
打印@输出
选择@output='Description:'+@Description
打印@输出
结束
其他的
开始
打印“sp_OAGetErrorInfo失败”
返回
结束
结束
--如果需要,请在每个步骤后执行一些错误处理。
--清理创建的对象。
执行官@hr=sp_OADestroy@iMsg
打印“已发送邮件!”
去
您是否检查了用于执行存储过程的?如果执行此操作:

exec@foo=dbo.SomeProc

然后
@foo
包含过程的返回状态,在本例中,如果成功,返回状态为零

SQL 2000确实有,因此您可以将存储过程执行字符串作为
@query
参数传递,以避免自己将结果集转换为标量值


您还应该考虑Perl、PerfS壳等语言中的外部脚本,因为使用纯TSQL方法格式化和发送电子邮件既不容易也不灵活。

我的SQL Server没有存储过程“XpSsEnmail”。我还没有找到xp#u sendmail SP…这就是我一直在使用上述的原因…我曾想过在C#中这样做,但不确定这是否会像一个sql作业集那样每天运行一次一样有效。
Create PROCEDURE [dbo].[sp_SQLNotify] 
   @From varchar(100) ,
   @To varchar(100) ,
   @Subject varchar(100)=" "

/*********************************************************************

This stored procedure takes the above parameters and sends an e-mail. 
All of the mail configurations are hard-coded in the stored procedure. 
Comments are added to the stored procedure where necessary.
Reference to the CDOSYS objects are at the following MSDN Web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_messaging.asp

***********************************************************************/ 
   AS
   Declare @iMsg int
   Declare @hr int
   Declare @source varchar(255)
   Declare @description varchar(500)
   Declare @output varchar(1000)
   Declare @Body Varchar (8000)



--************* Create the CDO.Message Object ************************
   EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT

--***************Configuring the Message Object ******************
-- This is to configure a remote SMTP server.
-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.asp
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
-- This is to configure the Server Name or IP address. 
-- Replace MailServerName by the name or IP of your SMTP Server.
   EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', '10.0.1.3' 

-- Save the configurations to the message object.
   EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null
   Exec @Body = sp_getpaidout1929

-- Set the e-mail parameters.
   EXEC @hr = sp_OASetProperty @iMsg, 'To', @To
   EXEC @hr = sp_OASetProperty @iMsg, 'From', @From
   EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @Subject

-- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'.
   EXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @Body
   EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL

-- Sample error handling.
   IF @hr <>0 
     select @hr
     BEGIN
       EXEC @hr = sp_OAGetErrorInfo NULL, @source OUT, @description OUT
       IF @hr = 0
         BEGIN
           SELECT @output = '  Source: ' + @source
           PRINT  @output
           SELECT @output = '  Description: ' + @description
           PRINT  @output
         END
       ELSE
         BEGIN
           PRINT '  sp_OAGetErrorInfo failed.'
           RETURN
         END
     END

-- Do some error handling after each step if you need to.
-- Clean up the objects created.
   EXEC @hr = sp_OADestroy @iMsg

   PRINT 'Mail Sent!'




GO