简体   繁体   English

每月的每个第一天在SQL Server 2012上通过电子邮件通过电子邮件附件发送查询结果

[英]Sending query results via email via email attachment every first day of every month on SQL Server 2012

My requirement: Send the query result via email attachment on first day of every month. 我的要求:每个月的第一天通过电子邮件附件发送查询结果。

The work I've been doing manually: 我一直在手动进行的工作:

  • I have to run this query every first day of each month by changing the date range. 我必须通过更改日期范围在每月的每个第一天运行此查询。
  • Then I export the result acquired in .csv format and send this csv file as an attachment 然后,我导出以.csv格式获取的结果,并将此csv文件作为附件发送

I needed suggestions from you people on how shall I automate this process: 我需要您的建议,以帮助我实现该过程的自动化:

  • Shall I set up a Job on SQL Server 2012, but yes, the I'll have to modify the date range. 我是否应该在SQL Server 2012上设置作业,但是可以,我必须修改日期范围。

Please suggest on how to move forward. 请提出前进的建议。

Any help, much appreciated. 任何帮助,不胜感激。

As you mentioned, Create a Job and schedule it to run on first day of every month. 如前所述,创建作业并将其安排为在每月的第一天运行。 Considering you have enough knowledge on creating a job. 考虑到您具有创造工作的足够知识。

Go to Job properties -> schedules -> and make the following setting 转到作业属性->时间表->进行以下设置

在此处输入图片说明

Occurs every first day of every 1 month(s) at 12:00:00. 每1个月的第一天的12:00:00发生。 Schedule will be used starting on 07-12-2016. 时间表将从2016年12月7日开始使用。

Change the timing(at which time it should run on first day of month) based on your business requirement. 根据您的业务需求更改时间(应该在每月的第一天运行)。 It can be set under Daily frequency-> Occurs once at: 可以在“ Daily frequency-> Occurs once at:下进行设置Daily frequency-> Occurs once at:

This process can also be automated by another way by using a Windows batch file.You can schedule it using Windows scheduler. 也可以使用Windows批处理文件以另一种方式使此过程自动化。您可以使用Windows调度程序对其进行调度。 Below will be contents of batch file 以下是批处理文件的内容

Echo off
sqlcmd -u <username> -p <password> -S <server name> -d <database name> -i <sql file location> -o   <output result file location>

Powershell.exe -executionpolicy remotesigned -File   <location of powershell file>

The powershell file trigger an email when bat file is run. 运行bat文件时,powershell文件会触发电子邮件。 Contents of powershell file Powershell文件的内容

$smtpserver = "<smtp server name>"
$from="mail id"
$to="<mail id"
$a = Get-Date
$subject= "<subject line> `r"+ $a.Month +"/"+$a.Day +"/"+ $a.Year
$body=""
$attachment="File location"   
Thanks,`n " 
$mailer = new-object Net.Mail.SMTPclient($smtpserver)
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body,$data1,$a)
$msg.IsBodyHTML = $true
$mailer.send($msg)

I use SQL Agent for send results via email like this: 我使用SQL Agent通过电子邮件发送结果,如下所示:

/*
First you should set up SQL Mail Profile.
Please change @dbName, @SQLscript, @mailbody and mail account values. When changing your @SQLscript value be careful that replace (with CTRL+H) single quota (') to double quotas ('').
*/
DECLARE @dbName nvarchar(50), @SQLscript nvarchar(4000), @subject varchar(100), @mailfrom varchar(100), @mailbody nvarchar(4000), @jobName varchar(100)
SELECT @jobName = name from msdb..sysjobs where job_id = $(ESCAPE_NONE(JOBID))
SELECT @mailfrom = @@SERVICENAME + ' <' + cast(SERVERPROPERTY('ComputerNamePhysicalNETBIOS') as varchar(50)) + '@domain.com>'
SELECT @subject = N'SQL Server Job Result [Job: ' + @jobName + ']'

SELECT @dbName = 'Database'
SELECT @SQLscript = '
INSERT INTO [Database].[Schema].[Table] (
  Column1
 ,Column2
 ) VALUES (
  ''Value1''
 ,''Value2'' ) 
'

SELECT @mailbody = N'
    Depending on case number 1234-5678-90 your script executed on <b>' + @@SERVERNAME + N'</b> instance and <b>' + @dbName + '</b> database. Script info and results are shown below. <br><br>' + 
    '<b>Script: </b><br>' + @SQLscript + '<br><br>' + 
    '<b>Result: </b><br>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'sqlmailprofile',
@recipients = '<mail1@domain.com>;<mail2@domain.com>',
@copy_recipients = '<mail3@domain.com>',
@from_address = @mailfrom,
@reply_to = '<mail3@domain.com>',
@subject = @subject,
@body = @mailbody,
@body_format = 'HTML',
@importance = 'HIGH',
@execute_query_database = @dbName,
@query = @SQLscript
/* If you want to send results with attached file:
@attach_query_result_as_file = 1,
@query_attachment_filename = 'script_output.csv',
@query_result_separator=@tab,
@query_result_width =32767,
@query_result_no_padding=1,
@exclude_query_output=1,
@query_result_header=1
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM