for xml path + HTML表格 +自动发送邮件 的疑惑
公司ERP服务器开启了SQL SERVER 自动发送邮件功能,使用情况良好。
DECLARE @html NVARCHAR(MAX)
set @html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body><table cellspacing="0" cellpadding="0" bordercolor="#CCCCCC" border="0" frame="hsides" rules="all" >'
SET @html = @html + CAST((
--未准时回签
select MA002+' ' td,MV002+' ' td,TC003 + ' ' td,TD001+'-'+TD002+'-'+TD003 + ' ' td,TD004 + ' ' td,TD005+' ' td,convert(int,TD008) td,' '+TD010 td
from COPTD JOIN COPTC ON TC001=TD001 AND TC002=TD002 JOIN COPMA ON MA001=TC004 LEFT JOIN CMSMV ON MV001=TC006
LEFT JOIN INVMB ON MB001=TD004
left join
(
select CTD001,CTD002,CTD003,C_ID,CTD006
FROM COPTD_delivery D1
where CTD006
IN (SELECT MAX(CTD006) FROM COPTD_delivery D2 where D2.CTD001=D1.CTD001 AND
D2.CTD002=D1.CTD002 AND D2.CTD003=D1.CTD003 )
) xxx on CTD001=TD001 AND CTD002=TD002 AND CTD003=TD003
WHERE DATEDIFF(DAY,TC003,convert(char(8),getdate(),112)) >=2 AND MB025 NOT IN ('C','P') AND CTD006 IS NULL AND TC003 >='20130701' and TD016='N' AND TD021='Y' and TD001 not IN ('2204','2205','2209')
order by TC003,MA002
for xml path ('tr')
) AS VARCHAR(MAX)) + '</table></body></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ERP_mail',
@recipients='', --邮件地址,多个;分开。
@subject = @message1,
@body = @html,
@body_format = 'HTMl' ;
end
故求助各位大神,怎么样能够将FOR XML PATH 查询出的结果,用类似HTML表格的格式发送,并且能让大部分邮箱支持。
拜谢。
[解决办法]
1.不用xml,如下
select '20130701' col1, '2202-20130700002-0001' col2,'50' col3,'PCS' col4
into #t
union all select '20130701','2212-20130700001-0001','216','PCS'
declare @tb varchar(1000)='';
select @tb=@tb+'<tr><td>'+col1+'</td><td>'+col2+'</td><td>'+col3+'</td><td>'+col4+'</td></tr>'
from #t
select '<table>'+@tb+'</table>'