首页 > 数据库 > SQL Server > 正文

SQL Server中发送HTML格式邮件的方法

2024-08-31 01:02:40
字体:
来源:转载
供稿:网友

这篇文章主要介绍了SQL Server中发送HTML格式邮件的方法,需要的朋友可以参考下

sql server 发送html格式的邮件,参考代码如下:

  1. DECLARE @tableHTML NVARCHAR(MAX) ; 
  2. -- 获取当前系统时间,和数据统计的时间 
  3. set @d_nowdate = convert(datetime,convert(varchar(10),dateadd(day,-1,getdate()),120),120); 
  4.  
  5. -- 如果有数据则发送 
  6. if exists (select top 1 * from t_table1(nolock) where d_rq=@d_nowdate) 
  7. begin 
  8. set @str_subject='某某'+convert(varchar(10),@d_nowdate,120)+'净值.'
  9. SET @tableHTML = N'<H1>某某</H1><br>目前测试中<br><table border="1">' + 
  10. N'<tr><th>日期</th><th>基金代码</th><th>基金名称</th><th>净值</th><th>累计净值</th></tr>' + 
  11. CAST ( (select convert(varchar(10),@d_nowdate,120) as 'td','',VC_JJDM as 'td','',Vc_jjmc as 'td','',EN_JJDWJZ as 'td','',EN_LJJZ as 'td' 
  12. from t_table1 t left join t_table2 tt on t.VC_JJDM = tt.C_FUNDCODE  
  13. where d_rq=@d_nowdate order by Vc_jjmc,VC_JJDM 
  14. FOR XML PATH('tr'), ELEMENTS-- TYPE  
  15. AS NVARCHAR(MAX) ) + N'</table>'
  16.  
  17. -- 发送邮件 
  18. exec @i_result = msdb.dbo.sp_send_dbmail 
  19. @profile_name = 'Profile-Mail'
  20. @recipients = '邮箱地址1;邮箱2;邮箱3',  
  21. @subject = @str_subject, 
  22. @body = @tableHTML, 
  23. @body_format = 'HTML'
  24. end 

邮件效果如下:

某某净值

目前测试中

日期

基金代码

基金名称

净值

累计净值

2013-12-20

111111

AAAAA

0.98300000

0.98300000

2013-12-20

222222

BBBBB

1.04900000

1.04900000

2013-12-20

333333

CCCCC

0.64000000

0.64000000

2013-12-20

444444

DDDDD

0.99400000

0.99400000

2013-12-20

555555

EEEEE

1.05700000

1.05700000

2013-12-20

666666

FFFFF

0.73400000

0.73400000

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表