溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》
  • 首頁 > 
  • 教程 > 
  • 數據庫 > 
  • SQL數據庫msdb.dbo.sp_send_dbmail函數發送郵件的示例分析

SQL數據庫msdb.dbo.sp_send_dbmail函數發送郵件的示例分析

發布時間:2021-07-30 11:08:55 來源:億速云 閱讀:132 作者:小新 欄目:數據庫

這篇文章主要介紹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 = '<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'

郵件發送的結果

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 = '<html><body><table><tr><td><p><font color="#000080" size="3" face="Verdana">您好!</font></p><p ><font size="3" face="Verdana">請審批下面的價格申請:</font></p></td></tr>';
--設置表頭
set @tableHTML=@tableHTML
+'<tr><td><table border="1" ><tr >
<th >RFQ No</th>
<th >sales</th>
<th >PL3</th>
<th >Customer</th>
<th >disty_name</th>
<th >2nd disty</th>
<th >Sold To Customer</th>
<th >Part No</th>
<th >Currency</th>
<th >Volume</th>
<th >Requested DC</th>
<th >Customer RP</th>
<th >Competitor</th>
<th >Competitor PN</th>
<th >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函數發送郵件的示例分析

以上是“SQL數據庫msdb.dbo.sp_send_dbmail函數發送郵件的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

sql
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女