在推行系统中,时不时会有用户提出希望系统能自动推送邮件,由于手头的工具和能力有限,不少需求都借助于sql server的邮件触发来实现。
步骤:
1、配置邮箱。步骤略,网上有不少帖子说明,手工直接在管理-数据库邮件配置即可。配置完成后可以右键测试邮箱是否正常工作。
2、制作发送邮件脚本
3、sql server 代理定义周期计划
邮件脚本编写:场景一:业务部门希望可以每周提供一次样品库存,即将sql查询的结果以附件的方式发给指定的人员。
EXEC msdb.dbo.sp_send_dbmail
@profile_name = '<账户名>', --定义好的sql server 邮箱账户名
@recipients = '<mail account>', --需要发送邮件的账号,多个用;间隔,建议通过一个邮件组来管理需要发送的地址
@body = 'The stored procedure finished successfully.', -- 邮件正文
@subject = '样品仓物料清单', --邮件抬头
@execute_query_database = 'UFDATA_001_2016', --查询的数据库 --需要执行的查询 @query = 'select
distinct substring(cinvcode,4,100) 料号
from
CurrentStock
where
cwhcode = 12
and iquantity >=1',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'item.csv'
邮件发送的结果
场景二,用户系统在OA系统完成的外部用户报备客户审批完成后触发邮件给对方。由于OA系统自动触发外部邮件格式有显示,据说需要js写代码,因为不熟悉,所以还是借助于sql server的邮件功能来实现。
预先写一个view,三个字段,需要发送的邮箱,邮件主题,邮件内容。
例子中将主题和主体做为一个,用到循环语句实现。
declare @mail nvarchar(200);
declare @note nvarchar(500); declare c cursor --游标
for select email,note from cux_dls_notice_v where operatedate + ' '+ operatetime >= DATEADD(MINUTE,-60,GETDATE()) --取最近一小时的记录发送,计划任务是60分钟执行一次。 open c
fetch next from c into @mail,@note; while @@FETCH_STATUS = 0 begin EXEC msdb.dbo.sp_send_dbmail
@profile_name= '<账户名>', --定义好的sql server 邮箱账户名
@recipients=@mail, --需要发送的邮箱
@subject=@note, --邮件标题
@body=@note --邮件主题
fetch next from c into @mail,@note;
end
close c;
deallocate c;
场景三,还是在OA系统里,销售申请特价之后提交审批,审批人系统可以收到邮件通知,并在邮件中和销售讨论后,再回到系统中审批。由于申请表的内容多,需要用html的发送格式。
做法和场景二类似,重点是邮件主题需要生成为html的格式。
还是一样把需要展现的内容做成一个view,我个人喜欢做view,这样有什么变化调整view就可以了。
/*声明变量*/
declare @tableHTML varchar(max)
declare @mail nvarchar(200);
declare @note nvarchar(500); --设置问候词
set @tableHTML = '<html><body><table><tr><td><p><font color="#000080" size="3" face="Verdana">您好!</font></p><p style="margin-left:30px;"><font size="3" face="Verdana">请审批下面的价格申请:</font></p></td></tr>';
--设置表头
set @tableHTML=@tableHTML
+'<tr><td><table border="1" style="border:1px solid #d5d5d5;border-collapse:collapse;border-spacing:0;margin-left:30px;margin-top:20px;"><tr style="height:25px;background-color: rgb(219, 240, 251);">
<th style="width:100px;">RFQ No</th>
<th style="width:200px;">sales</th>
<th style="width:60px;">PL3</th>
<th style="width:80px;">Customer</th>
<th style="width:100px;">disty_name</th>
<th style="width:60px;">2nd disty</th>
<th style="width:80px;">Sold To Customer</th>
<th style="width:80px;">Part No</th>
<th style="width:100px;">Currency</th>
<th style="width:60px;">Volume</th>
<th style="width:100px;">Requested DC</th>
<th style="width:100px;">Customer RP</th>
<th style="width:100px;">Competitor</th>
<th style="width:100px;">Competitor PN</th>
<th style="width:80px;">Competitor Price</th></tr>'; --启用游标
declare c cursor for --查询结果 select
a.email
,a.note
,@tableHTML+'<tr><td align="center">'+rfq_quotation_number+'</td>'
+'<td align="center">'+lastname+'</td>'
+'<td align="center">'+pl3+'</td>'
+'<td align="center">'+customer+'</td>'
+'<td align="center">'+disty_name+'</td>'
+'<td align="center">'+snd_disty+'</td>'
+'<td align="center">'+sold_to_customer+'</td>'
+'<td align="center">'+fully_part_no+'</td>'
+'<td align="center">'+currency+'</td>'
+'<td align="center">'+volume+'</td>'
+'<td align="center">'+requested_disty_cost+'</td>'
+'<td align="center">'+cust_requested_price+'</td>'
+'<td align="center">'+competitor+'</td>'
+'<td align="center">'+competitor_part_no+'</td>'
+'<td align="center">'+Competitor_Price+'</td></tr>'
from
(
select
,note
,rfq_quotation_number
,lastname
,pl3
,客户中文+'/'+客户英文 as customer
,disty_name
,snd_disty
,sold_to_customer
,fully_part_no
,currency
,isnull(cast(volume as nvarchar(10)),'') volume
,isnull(cast(requested_disty_cost as varchar(10)),'') requested_disty_cost
,isnull(cast(cust_requested_price as varchar(10)),'') as cust_requested_price
,isnull(cast(competitor as varchar(100)),'') competitor
,isnull(cast(competitor_part_no as varchar(50)),'') competitor_part_no
,isnull(cast(competitor_price as varchar(10)),'') competitor_price
from cux_rfq_v
where currentnodetype = 1 and lastoperatedate + ' '+ lastoperatetime >= DATEADD(MINUTE,-60,GETDATE()) --找最近60分的记录,并发送
) a open c fetch next from c into
,@note
,@tableHTML; while @@FETCH_STATUS = 0 begin EXEC msdb.dbo.sp_send_dbmail
@profile_name= '<账户名>', --定义好的sql server 邮箱账户名
,@recipients=@mail
,@subject=@note
,@body= @tableHTML
,@body_format='HTML'
fetch next from c into
,@note
,@tableHTML;
end
close c;
deallocate c;
场景四,场景四是三的升级版,由于查询的结果多,将结果已表格的形式发送邮件
/*声明变量*/
declare @tableHTML varchar(max)
declare @mail nvarchar(200);
declare @note nvarchar(500); --设置问候词
set @tableHTML = '<html><body><table><tr><td><p><font color="#000080" size="3" face="Verdana">Hello!</font></p>
<p style="margin-left:30px;"><font size="3" face="Verdana">
Pls respond to a Email to ensure your team have uploaded the latest test program to your network server from the specific link after you receiving the notice.Relevant information refer to the attachment,thanks!</font></p>
<p style="margin-left:30px;"><font size="3" face="Verdana">
Noted:Please make sure the whole wafer lot online have completed FT before using new program,thanks!
</font></p></td></tr>';
--设置格式
set @tableHTML=@tableHTML
--+'<tr><td><table border="1" style="border:1px solid #d5d5d5;border-collapse:collapse;border-spacing:0;margin-left:30px;margin-top:20px;"><tr style="height:25px;background-color: rgb(219, 240, 251);"></tr>';
+'<table border="1">' --启用游标
declare c cursor for --查询结果 select
a.email
,'Mass-production Program Release Notice' as note
--邮件表格主体
,@tableHTML
+'<tr><td align="center" bgcolor=#A6FFFF>'+'流程发布号'+'</td>'+'<td align="center">'+a.release_no+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'申请人'+'</td>'+'<td align="center">'+a.apply_by+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'申请日期'+'</td>'+'<td align="center">'+a.apply_when+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'发布类型'+'</td>'+'<td align="center">'+a.apply_type+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'测试程序'+'</td>'+'<td align="center">'+a.[test_program]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'测试程序地址'+'</td>'+'<td align="center">'+a.[test_program_address]+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'封测厂'+'</td>'+'<td align="center">'+a.[encapsulation]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Tester'+'</td>'+'<td align="center">'+a.[Tester]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Handler'+'</td>'+'<td align="center">'+a.[Handler]+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'Site'+'</td>'+'<td align="center">'+a.site+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Test Time(ms)'+'</td>'+'<td align="center">'+a.[Test_Time]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Index Time(ms)'+'</td>'+'<td align="center">'+a.[Index_Time]+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'Load Board'+'</td>'+'<td align="center">'+a.[Load_board]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Load Board Rev.'+'</td>'+'<td align="center">'+a.[Load_Board_Rev]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Yeild Flag(%)'+'</td>'+'<td align="center">'+a.[Yeild_Flag]+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'Dut Board'+'</td>'+'<td align="center">'+a.[Dut_board]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Dut Board Rev'+'</td>'+'<td align="center">'+a.[Dut_Board_Rev]+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'O/S Flag(%)'+'</td>'+'<td align="center">'+a.[OS_Flag]+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'UPH(K)'+'</td>'+'<td align="center">'+a.UPH+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Gold Sample'+'</td>'+'<td align="center">'+a.Golden_Sample+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'Pass Bin'+'</td>'+'<td align="center">'+a.Pass_Bin+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'P/N'+'</td>'+'<td align="center">'+a.part_no+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'封装'+'</td>'+'<td align="center">'+a.sta+'</td>'+'<td align="center" bgcolor=#A6FFFF>'+'TE'+'</td>'+'<td align="center">'+a.te+'</td></tr>'
+'<td align="center" bgcolor=#A6FFFF>'+'Remark'+'</td>'+'<td align="left" th colspan="5">'+a.Remark+'</td>' -- 跨越5列
+'</table>'
from
(
SELECT
[email]
,[release_no]
,[currentnodeid]
,[requestname]
,[currentnodetype]
,[createdate]
,[createtime]
,[apply_by]
,[apply_when]
,[apply_type]
,[test_program]
,[test_program_address]
,[encapsulation]
,[Tester]
,[Handler]
,cast([site] as varchar(20)) as site
,cast([Test_Time] as varchar(20)) as test_time
,cast([Index_Time] as varchar(20)) as index_time
,cast([Load_board] as varchar(20)) as load_board
,cast([Load_Board_Rev] as varchar(20)) as Load_Board_Rev
,cast([Yeild_Flag] as varchar(20)) as Yeild_Flag
,cast([Dut_board] as varchar(20)) as Dut_board
,cast([Dut_Board_Rev] as varchar(20)) as Dut_Board_Rev
,cast([OS_Flag] as varchar(20)) as OS_Flag
,cast([UPH] as varchar(20)) as UPH
,cast([Golden_Sample] as varchar(20)) as Golden_Sample
,cast([Pass_Bin] as varchar(20)) as Pass_Bin
,part_no
,[sta]
,te
,cast(Remark as varchar(1000)) as remark
FROM [cux_Mass_production_v]
--where currentnodetype = 1 and lastoperatedate + ' '+ lastoperatetime >= DATEADD(MINUTE,-60,GETDATE()) --找最近60分的记录,并发送
) a open c fetch next from c into
,@note
,@tableHTML; while @@FETCH_STATUS = 0 begin EXEC msdb.dbo.sp_send_dbmail
@profile_name= 'e-cology' --定义好的sql server 邮箱账户名
,@recipients=@mail
,@subject=@note
,@body= @tableHTML
,@body_format='HTML'
fetch next from c into
,@note
,@tableHTML;
end
close c;
deallocate c;
场景五:制作定期发送通知给相关人员,告知其在系统中未处理的流程。
/*声明变量*/
declare @tableHTML varchar(max)
declare @mail nvarchar(200);
declare @note nvarchar(500);
declare @name nvarchar(100); --启用游标
declare c cursor for
SELECT distinct email,approver FROM cux_workflow_info_v
open c
fetch next from c into @mail,@name; while @@FETCH_STATUS = 0 begin --生成查询的结果 --设置问候词
set @tableHTML ='<html><body><table><tr><td><p>'+@name;
set @tableHTML = @tableHTML+',</p><p style="margin-left:30px;">您好!请尽快处理泛微系统中的流程</p>'
--设置表头
set @tableHTML=@tableHTML+
'<tr><td><table border="1" style="border:1px solid #d5d5d5;border-collapse:collapse;border-spacing:0;margin-left:30px;margin-top:20px;"><tr style="height:25px;background-color: rgb(219, 240, 251);">
<th>流程名称</th><th>收到时间</th><th>延迟天数</th></tr>'; set @tableHTML=@tableHTML--+'<tr>'
+ CAST((
SELECT
td=requestname,'',
td=lastoperatedate,'',
td=delayday,''
FROM cux_workflow_info_v
where email=@mail
for xml path('tr'),type) as varchar(max))
+N'</table>' EXEC msdb.dbo.sp_send_dbmail
@profile_name= 'e-cology' --定义好的sql server 邮箱账户名
,@recipients=@mail
,@subject='请及时处理OA系统中的流程!'
,@body= @tableHTML
,@body_format='HTML' set @tableHTML = ''
fetch next from c into @mail,@name;
end
close c;
deallocate c;