公司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
这样发出去的邮件格式:
20130701 2202-20130700002-0001 50 PCS
20130701 2212-20130700001-0001 216 PCS
本来按照设想整个表是一个table, 一个td就是一列的,可现在所有的列都合并到了一起,因为for xml path 默认将相同名称的节点td合并。
无奈之下网上查资料,发现XML文件引用xsl模板,可以实现想要的效果,可本地成功之后发到邮箱,发现邮箱不支持XML+XSL,这下没辙了。