【问题标题】:How to email query output如何通过电子邮件发送查询输出
【发布时间】:2012-07-11 02:20:38
【问题描述】:

我有一个基本查询,我想通过电子邮件发送结果。如何在查询级别执行此操作?所以如果我的查询是:

SELECT 
    Store_Id, Paid_Out_Amount, Paid_Out_Comment, 
    Paid_Out_Datetime, Update_UserName
FROM Paid_Out_Tb
WHERE (Store_Id = 1929) OR
      (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))

如何通过电子邮件发送输出?我有一个通过 SMTP 发送电子邮件的程序,参数是 @From, @To, @Subject@body... 有效。我如何让正文成为查询的结果?

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[sp_SQLNotify] 
   @From varchar(100) ,
   @To varchar(100) ,
   @Subject varchar(100)=" ",
   @Body varchar(4000) = "Test"
/*********************************************************************

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)

--************* 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.1.1.10' 

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

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


-- Clean up the objects created.
   EXEC @hr = sp_OADestroy @iMsg

   PRINT 'Mail Sent!'



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

这是我用来发送邮件的过程...有效...我只想在其中添加一个点以将查询结果发送到上面...这可以在在程序中?

【问题讨论】:

  • 是要将整个查询输出发送给一个人,还是要将每一行发送给不同的人?
  • 我想将整个输出发送给一个人...通常只有 5 行输出...
  • 问题是它是 Server 2000... 这意味着 sp_send_dbmail 过程将无法工作... 不知道如何完成它。

标签: sql stored-procedures smtp sql-server-2000


【解决方案1】:

使用 SQL Server Powershell 包。 here 是一个使用它来获取输出的例子(附有详细解释)。

(以上内容来自this SO answer,但为了澄清她/他所说的:需要 SQL Server 2008 客户端组件(Express 应该没问题),但它可以与 SQL Server 2000 数据库一起使用 (source) .)

【讨论】:

  • 这似乎比我想象的要复杂。我最初的想法是必须有某种向后兼容的发送邮件程序......或者至少有一种方法可以添加一种方法来获取查询结果并从程序中通过电子邮件发送它们......现在我有一个通过电子邮件发送的程序很好,只是不使用结果...
【解决方案2】:

您还可以将变量用于直接循环连接。请参阅https://stackoverflow.com/a/4447564/1180926(尽管您将使用制表符和换行符而不是 HTML 代码)。

然后您只需相应地更改您的查询,将其存储在@Body 中,就完成了!

【讨论】:

  • 我可以调用我的过程并将@body 分配给查询吗?
  • 不,你必须使用那个字符串技巧。您只能将查询的最后一行分配给字符串 - 变量连接是一种解决方法。
猜你喜欢
  • 1970-01-01
  • 2014-04-13
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
相关资源
最近更新 更多