關(guān)于SQL數(shù)據(jù)庫(kù) msdb.dbo.sp
在推行系統(tǒng)中,時(shí)不時(shí)會(huì)有用戶提出希望系統(tǒng)能自動(dòng)推送郵件,由于手頭的工具和能力有限,不少需求都借助于sql server的郵件觸發(fā)來(lái)實(shí)現(xiàn)。
步驟:
1、配置郵箱。步驟略,網(wǎng)上有不少帖子說(shuō)明,手工直接在管理-數(shù)據(jù)庫(kù)郵件配置即可。配置完成后可以右鍵測(cè)試郵箱是否正常工作。
2、制作發(fā)送郵件腳本
3、sql server 代理定義周期計(jì)劃
郵件腳本編寫(xiě):
場(chǎng)景一:業(yè)務(wù)部門(mén)希望可以每周提供一次樣品庫(kù)存,即將sql查詢的結(jié)果以附件的方式發(fā)給指定的人員。
EXEC msdb.dbo.sp_send_dbmail @profile_name = '<賬戶名>', --定義好的sql server 郵箱賬戶名 @recipients = '<mail account>', --需要發(fā)送郵件的賬號(hào),多個(gè)用;間隔,建議通過(guò)一個(gè)郵件組來(lái)管理需要發(fā)送的地址 @body = 'The stored procedure finished successfully.', -- 郵件正文 @subject = '樣品倉(cāng)物料清單', --郵件抬頭 @execute_query_database = 'UFDATA_001_2016', --查詢的數(shù)據(jù)庫(kù) --需要執(zhí)行的查詢 @query = 'select distinct substring(cinvcode,4,100) 料號(hào) from CurrentStock where cwhcode = 12 and iquantity >=1', @attach_query_result_as_file = 1, @query_attachment_filename = 'item.csv'
郵件發(fā)送的結(jié)果
場(chǎng)景二,用戶系統(tǒng)在OA系統(tǒng)完成的外部用戶報(bào)備客戶審批完成后觸發(fā)郵件給對(duì)方。由于OA系統(tǒng)自動(dòng)觸發(fā)外部郵件格式有顯示,據(jù)說(shuō)需要js寫(xiě)代碼,因?yàn)椴皇煜ぃ赃€是借助于sql server的郵件功能來(lái)實(shí)現(xiàn)。
預(yù)先寫(xiě)一個(gè)view,三個(gè)字段,需要發(fā)送的郵箱,郵件主題,郵件內(nèi)容。
例子中將主題和主體做為一個(gè),用到循環(huán)語(yǔ)句實(shí)現(xiàn)。
declare @mail nvarchar(200); declare @note nvarchar(500); declare c cursor --游標(biāo) for select email,note from cux_dls_notice_v where operatedate + ' '+ operatetime >= DATEADD(MINUTE,-60,GETDATE()) --取最近一小時(shí)的記錄發(fā)送,計(jì)劃任務(wù)是60分鐘執(zhí)行一次。 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, --需要發(fā)送的郵箱 @subject=@note, --郵件標(biāo)題 @body=@note --郵件主題 fetch next from c into @mail,@note; end close c; deallocate c;
場(chǎng)景三,還是在OA系統(tǒng)里,銷(xiāo)售申請(qǐng)?zhí)貎r(jià)之后提交審批,審批人系統(tǒng)可以收到郵件通知,并在郵件中和銷(xiāo)售討論后,再回到系統(tǒng)中審批。由于申請(qǐng)表的內(nèi)容多,需要用html的發(fā)送格式。
做法和場(chǎng)景二類(lèi)似,重點(diǎn)是郵件主題需要生成為html的格式。
還是一樣把需要展現(xiàn)的內(nèi)容做成一個(gè)view,我個(gè)人喜歡做view,這樣有什么變化調(diào)整view就可以了。
/*聲明變量*/ declare @tableHTML varchar(max) declare @mail nvarchar(200); declare @note nvarchar(500); --設(shè)置問(wèn)候詞 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">請(qǐng)審批下面的價(jià)格申請(qǐng):</font></p></td></tr>'; --設(shè)置表頭 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>'; --啟用游標(biāo) declare c cursor for --查詢結(jié)果 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分的記錄,并發(fā)送 ) 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;
總結(jié)
以上所述是小編給大家介紹的關(guān)于SQL數(shù)據(jù)庫(kù) msdb.dbo.sp_send_dbmail 函數(shù)發(fā)送郵件的場(chǎng)景分析,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
上一篇:SQL Server四個(gè)系統(tǒng)表的知識(shí)講解
欄 目:MsSql
本文標(biāo)題:關(guān)于SQL數(shù)據(jù)庫(kù) msdb.dbo.sp
本文地址:http://mengdiqiu.com.cn/a1/MsSql/10331.html
您可能感興趣的文章
- 01-10SQLServer存儲(chǔ)過(guò)程實(shí)現(xiàn)單條件分頁(yè)
- 01-10SQL Server 2012降級(jí)至2008R2的方法
- 01-10SQLServer中防止并發(fā)插入重復(fù)數(shù)據(jù)的方法詳解
- 01-10SQL Server數(shù)據(jù)庫(kù)定時(shí)自動(dòng)備份
- 01-10SQL Server性能調(diào)優(yōu)之緩存
- 01-10實(shí)現(xiàn)SQL Server 原生數(shù)據(jù)從XML生成JSON數(shù)據(jù)的實(shí)例代碼
- 01-10Sql Server 死鎖的監(jiān)控分析解決思路
- 01-10SqlServer 在事務(wù)中獲得自增ID的實(shí)例代碼
- 01-10SqlServer快速檢索某個(gè)字段在哪些存儲(chǔ)過(guò)程中(sql 語(yǔ)句)
- 01-10SQLServer性能優(yōu)化--間接實(shí)現(xiàn)函數(shù)索引或者Hash索引


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10SQLServer存儲(chǔ)過(guò)程實(shí)現(xiàn)單條件分頁(yè)
- 01-10SQLServer中防止并發(fā)插入重復(fù)數(shù)據(jù)的方
- 01-10SQL Server 2012降級(jí)至2008R2的方法
- 01-10SQL Server性能調(diào)優(yōu)之緩存
- 01-10SQL Server數(shù)據(jù)庫(kù)定時(shí)自動(dòng)備份
- 01-10Sql Server 死鎖的監(jiān)控分析解決思路
- 01-10實(shí)現(xiàn)SQL Server 原生數(shù)據(jù)從XML生成JSON數(shù)
- 01-10SqlServer快速檢索某個(gè)字段在哪些存儲(chǔ)
- 01-10SqlServer 在事務(wù)中獲得自增ID的實(shí)例代
- 01-10SQLServer性能優(yōu)化--間接實(shí)現(xiàn)函數(shù)索引或
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子