如何将一个简单的select语句转换为sqlserver代理作业以进行调度和自动发送电子邮件

rwqw0loc  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(268)

如何将简单的select语句转换为SQLServer代理作业,以便自动向操作员发送电子邮件。例如 USE [DYNAMICS] select * from ACTIVITY 我每天上午10:00通过电子邮件需要这个活动表的内容。如何从SQLServer代理自动发送电子邮件?需要帮助。

enyaitl3

enyaitl31#

sql server有一个函数sp\u send\u dbmail,这会有所帮助。https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql?view=sql-server-ver15
配置服务器:

execute sp_configure 'Show Advanced Options', 1
reconfigure
execute sp_configure 'Database Mail XPs', 1
reconfigure

配置您的帐户:

execute msdb.dbo.sysmail_add_account_sp
@mailserver_name = 'smtp.gmail.com',
@port = 587,
@enable_ssl = 1,
@account_name = 'mail_account', 
@display_name = 'SQL SERVER',
@email_address = 'your@email.com',
@username = 'your@login',
@password = '***password***'

execute msdb.dbo.sysmail_add_profile_sp
    @profile_name = 'mail_profile',
    @description = 'Mail notification by SQL.'

execute msdb.dbo.sysmail_add_profileaccount_sp
    @profile_name = 'mail_profile',
    @account_name = 'mail_account',
    @sequence_number = 1

发送邮件

execute msdb.dbo.sp_send_dbmail 
    @profile_name = 'MAIL NAME',
    @recipients = 'mailto@mailto.com',
    @subject = 'subject',
    @body = 'your message'

就这些!

相关问题