- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
在推行系统中,时不时会有用户提出希望系统能自动推送邮件,由于手头的工具和能力有限,不少需求都借助于sql server的邮件触发来实现.
步骤:
1、配置邮箱。步骤略,网上有不少帖子说明,手工直接在管理-数据库邮件配置即可。配置完成后可以右键测试邮箱是否正常工作.
2、制作发送邮件脚本 。
3、sql server 代理定义周期计划 。
邮件脚本编写:
场景一:业务部门希望可以每周提供一次样品库存,即将sql查询的结果以附件的方式发给指定的人员.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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,三个字段,需要发送的邮箱,邮件主题,邮件内容.
例子中将主题和主体做为一个,用到循环语句实现.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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就可以了.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/*声明变量*/
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
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 函数发送邮件的场景分析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:https://www.cnblogs.com/celia0208/archive/2018/10/25/9849304.html 。
最后此篇关于关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析的文章就讲到这里了,如果你想了解更多关于关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试使用 sp_send_dbmail 发送附件。附件存储在表的 varbinary(MAX) 列中。这是我的查询: EXEC msdb.dbo.sp_send_dbmail @
我正在研究一个 SQL 存储过程,它应该发送带有查询结果的附件。 我正在使用 sp_send_dbmail发送电子邮件。 在我想发送的查询中,我将 加入到一个表变量中。当我执行存储过程时,我收到一条错
我在 SQL Server 2008 中使用 sp_send_dbmail 发送查询结果。我将查询移动到一个过程中,并尝试在 sp_send_dbmail 过程中使用该过程,如下所示: EXEC ms
我使用的是 SQL Server 2008 R2。当我执行 sp_send_dbmail 时,我无法更改 DBA 指定的默认地址 FROM_ADDRESS。如果不起作用为什么还有这个参数? 如何指定“
我在此 MSDN 页面上使用类似于示例 C 的内容: http://msdn.microsoft.com/en-us/library/ms190307.aspx DECLARE @tableHTML
我已经写了一个SQL查询。但这给了我一个错误。 DECLARE @Delimiter Char(1) SET @Delimiter = CHAR(9) EXEC MSDB.dbo.sp_Send_DB
我正在尝试使用 sp_send_dbmail 发送电子邮件。我需要它将一个查询作为附件发送,另一个查询作为电子邮件正文的一部分发送。问题是 sp_send_dbmail 只有一个 @query 参数,
我需要发送带有文件附件的电子邮件。附件必须来自图像类型数据字段。在查询窗口和存储过程中运行以下代码时,不会发送电子邮件。在查询窗口中,它只显示“命令成功完成”,但没有电子邮件。 EXEC msdb.
我有一个每天晚上运行的存储过程,它应该将查询结果发送给多个收件人。然而,在大多数情况下,它最终会在一分钟后发送一封重复的电子邮件。我使用的代码如下(所有电子邮件和数据库引用均已更改): EXEC ms
正如标题所说,我正在尝试制作一个存储过程,用于将电子邮件发送到数据库中存储的地址,通常包括多个收件人。我已经尝试了几种方法来解决这个问题,但我认为我正在碰壁,我会很感激任何人提供的任何意见。我遇到的主
有没有办法为我的数据库中的用户提供访问权限以执行 msdb.dbo.sp_send_dbmail 无需将它们添加到 MSDB 数据库和 DatabaseMailUserRole? 我试过这个: ALT
我在 SQL2005 中使用 sp_send_dbmail 发送带有附件中结果的电子邮件。发送附件时,它是 UCS-2 编码的,我希望它是 ANSI 或 UTF-8。 这是 SQL EXEC msdb
我们使用 sp_send_dbmail 来通知我们某些问题,有时我们会在电子邮件中附加文件。有时我们会收到错误 32(文件正在使用)错误。在这种情况下,我已经将一些代码放在一起发送没有附件的电子邮件,
我正在使用 SQL Server 的 sp_send_dbmail 存储过程通过数据库发送邮件。 但是当我执行该程序时,它会关闭数据库邮件程序。我尝试通过 sysmail_start_sp 再次启动它
我在 sp_send_dbmail 中使用 @query 边界发送一封包含警告列表的电子邮件(警告由 @query 边界返回)。 @query 边界在电子邮件中以文本形式列出。从 @query 边界返
我正在使用 msdb.dbo.sp_send_dbmail 发送电子邮件。有时电子邮件不发送。我有失败的电子邮件列表。 SELECT TOP 10 * from msdb.dbo.sysmail_ev
我收到此错误: Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1 The EXECUTE permission was deni
我正在尝试在 SQL Server 2008 中创建一个存储过程,用于发送插入到出站表中的电子邮件。我正在使用 sp_send_dbmail。它使用游标遍历出站表。我还想删除包含我已发送的电子邮件的记
我正在使用 msdb.dbo.sp_send_dbmail 发送电子邮件。有时电子邮件不发送。我有失败的电子邮件列表。 SELECT TOP 10 * from msdb.dbo.sysmail_ev
我正在寻找一种方法来检查使用 sp_send_dbmail 排队的特定电子邮件是否最终从我们的 Exchange 服务器成功发送。我查看了系统表 msdb.dbo.sysmail_mailitems
我是一名优秀的程序员,十分优秀!