0

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

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

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

4

2 回答 2

0

Use the SQL Server Powershell pack. An example (with detailed explanation) of using it to obtain output is here.

(The above is taken from this SO answer, but to clarify something s/he says: SQL Server 2008 client components is required (Express should be fine), but it can work with SQL Server 2000 databases (source).)

于 2012-07-10T05:23:02.803 回答
0

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

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

于 2012-07-10T18:20:38.150 回答