1. 个人技术分享首页

关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析

在推行系统中,时不时会有用户提出希望系统能自动推送邮件,由于手头的工具和能力有限,不少需求都借助于sql server的邮件触发来实现。

步骤:

1、配置邮箱。步骤略,网上有不少帖子说明,手工直接在管理-数据库邮件配置即可。配置完成后可以右键测试邮箱是否正常工作。

2、制作发送邮件脚本

3、sql server 代理定义周期计划

邮件脚本编写:

场景一:业务部门希望可以每周提供一次样品库存,即将sql查询的结果以附件的方式发给指定的人员。

 EXEC msdb.dbo.sp_send_dbmail
   @profile_name = '', --定义好的sql server 邮箱账户名
   @recipients = '', --需要发送邮件的账号,多个用;间隔,建议通过一个邮件组来管理需要发送的地址
   @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'

邮件发送的结果

关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析

场景二,用户系统在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;

关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析

场景三,还是在OA系统里,销售申请特价之后提交审批,审批人系统可以收到邮件通知,并在邮件中和销售讨论后,再回到系统中审批。由于申请表的内容多,需要用html的发送格式。

做法和场景二类似,重点是邮件主题需要生成为html的格式。

还是一样把需要展现的内容做成一个view,我个人喜欢做view,这样有什么变化调整view就可以了。

/*声明变量*/
declare @tableHTML varchar(max)
declare @mail nvarchar(200);
declare @note nvarchar(500);
--设置问候词
set @tableHTML = '

‘;
–设置表头
set @tableHTML=@tableHTML
+’

您好!

请审批下面的价格申请:

‘;
–启用游标
declare c cursor for
–查询结果
select
a.email
,a.note
,@tableHTML+’


+’


+’


+’


+’


+’


+’


+’


+’


+’


+’


+’


+’


+’


+’


from
(
select
email
,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
@mail
,@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
@mail
,@note
,@tableHTML;
end
close c;
deallocate c;

关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析

总结

以上所述是小编给大家介绍的关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

最新评论

/*更多导航*/
$(“#nav p”).hover(function(){$(this).addClass(“hover”)},function(){$(this).removeClass(“hover”)});
if (top.location != self.location)top.location=self.location;
var varwindow=$(window);
$(‘#content’).find(‘img’).each(function(){
var img = this;
if (img.width >= 800 && !$(this).hasClass(“nohref”)){
img.style.width = “800px”;
img.style.height = “auto”;
}
});
$(window).scroll(function()
{
//rFixedBox跟随滚动
var h = varwindow.height();
var top = varwindow.scrollTop();
var rFixedBox = $(‘.rFixedBox’).prev().offset();
var fixedTop = rFixedBox.top;

if(top>=fixedTop+330)
{
var h1 = parseInt($(‘#content’).children(‘.main’).height());
$(‘.rFixedBox’).css({‘position’:’fixed’,’top’: 0});
}
else
{
$(‘.rFixedBox’).css({‘position’:’static’, ‘top’:0});
}
/* return true;*/

/*右侧快捷菜单*/
sideFixed();
});

var ww = varwindow.width();
function sideFixed(){
var scrolltop = document.body.scrollTop || document.documentElement.scrollTop;
var a1 = 550;
if(ww>440){
if(a11500){
if(!window.suoyinobj){
window.suoyinobj=new katelog({
contentEl: ‘content’,
catelogEl: ‘CatelogList’,
linkClass: ‘AutoCatelogLink’,
linkActiveClass: ‘CatelogActive’,
supplyTop: 20,
selector: [‘h2′,’h3′,’h4’],
active: function (el) {
//console.log(el);
}
});
}
}else{
window.suoyinobj=null;
GenerateContentList();
}
}
}
show_suoyin();

SyntaxHighlighter.autoloader(
‘applescript /jslib/syntaxhighlighter/scripts/shBrushAppleScript.js’,
‘actionscript3 as3 /jslib/syntaxhighlighter/scripts/shBrushAS3.js’,
‘bash shell /jslib/syntaxhighlighter/scripts/shBrushBash.js’,
‘coldfusion cf /jslib/syntaxhighlighter/scripts/shBrushColdFusion.js’,
‘cpp c /jslib/syntaxhighlighter/scripts/shBrushCpp.js’,
‘obj-c objc /jslib/syntaxhighlighter/scripts/shBrushObjC.js’,
‘c# c-sharp csharp /jslib/syntaxhighlighter/scripts/shBrushCSharp.js’,
‘css /jslib/syntaxhighlighter/scripts/shBrushCss.js’,
‘delphi pascal /jslib/syntaxhighlighter/scripts/shBrushDelphi.js’,
‘diff patch pas /jslib/syntaxhighlighter/scripts/shBrushDiff.js’,
‘erl erlang /jslib/syntaxhighlighter/scripts/shBrushErlang.js’,
‘groovy /jslib/syntaxhighlighter/scripts/shBrushGroovy.js’,
‘haxe hx /jslib/syntaxhighlighter/scripts/shBrushHaxe.js’,
‘java /jslib/syntaxhighlighter/scripts/shBrushJava.js’,
‘jfx javafx /jslib/syntaxhighlighter/scripts/shBrushJavaFX.js’,
‘js jscript javascript /jslib/syntaxhighlighter/scripts/shBrushJScript.js’,
‘perl pl /jslib/syntaxhighlighter/scripts/shBrushPerl.js’,
‘php /jslib/syntaxhighlighter/scripts/shBrushPhp.js’,
‘text plain /jslib/syntaxhighlighter/scripts/shBrushPlain.js’,
‘py python /jslib/syntaxhighlighter/scripts/shBrushPython.js’,
‘ruby rails ror rb /jslib/syntaxhighlighter/scripts/shBrushRuby.js’,
‘scala /jslib/syntaxhighlighter/scripts/shBrushScala.js’,
‘sql /jslib/syntaxhighlighter/scripts/shBrushSql.js’,
‘vb vbnet /jslib/syntaxhighlighter/scripts/shBrushVb.js’,
‘ps powershell /jslib/syntaxhighlighter/scripts/shBrushPowerShell.js’,
‘xml xhtml xslt html /jslib/syntaxhighlighter/scripts/shBrushXml.js’
);
SyntaxHighlighter.all();

var ourl=”https://www.cnblogs.com/celia0208/archive/2018/10/25/9849304.html”;

(function(){
var bp = document.createElement(‘script’);
var curProtocol = window.location.protocol.split(‘:’)[0];
if (curProtocol === ‘https’) {
bp.src = ‘https://zz.bdstatic.com/linksubmit/push.js’;
}
else {
bp.src = ‘http://push.zhanzhang.baidu.com/push.js’;
}
var s = document.getElementsByTagName(“script”)[0];
s.parentNode.insertBefore(bp, s);
})();
jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);jbLoader();jbLoader(true);

if (‘undefined’ == typeof(window.Viewer)) {
document.write(unescape(“%3Cscript src=’/skin/js/viewer.min.js’ type=’text/javascript’%3E%3C/script%3E”));
}
var viewer = new Viewer(document.getElementById(‘content’));
//tu+
var cpro_id = “u1893014″;
document.write(”+’cript>’);
(function(){
var src = (document.location.protocol == “http:”) ? “http://js.passport.qihucdn.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba”:”https://jspassport.ssl.qhimg.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba”;
document.write(”);
})();

{
“@context”: “https://ziyuan.baidu.com/contexts/cambrian.jsonld”,
“@id”: “https://www.jb51.net/article/149522.htm”,
“appid”: “1549322409310619”,
“title”: “关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析”,
“description”: “这篇文章主要介绍了关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析,需要的朋友可以参考下”,
“pubDate”: “2018-10-25T14:27:34”,
“upDate”: “2018-10-25T14:27:35”
}

RFQ No sales PL3 Customer disty_name 2nd disty Sold To Customer Part No Currency Volume Requested DC Customer RP Competitor Competitor PN Competitor Price
‘+rfq_quotation_number+’ ‘+lastname+’ ‘+pl3+’ ‘+customer+’ ‘+disty_name+’ ‘+snd_disty+’ ‘+sold_to_customer+’ ‘+fully_part_no+’ ‘+currency+’ ‘+volume+’ ‘+requested_disty_cost+’ ‘+cust_requested_price+’ ‘+competitor+’ ‘+competitor_part_no+’ ‘+Competitor_Price+’

文章来源于互联网:关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析

原创文章,作者:admin,如若转载,请注明出处:https://www.aliyunsolution.com/2801.html