SQL数据库邮件服务

时间:2019-03-20 06:04:47

标签: sql-server tsql sqlmail

在SQL Server中,我编写了一个脚本,用于从SQL数据库发送邮件,在该脚本中,我使用xml将数据转换为正确的表格式。现在我想在多个查询的邮件中显示多个表。 随附一些示例文档供参考,请找到它,并让我知道如何获取所需的输出。如果还附带了欲望输出示例,请告诉我是否有人找到了解决方法

enter image description here

DECLARE @xml NVARCHAR(MAX)
DECLARE @xmlP NVARCHAR(MAX)
DECLARE @xmlI NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
DECLARE @body1 NVARCHAR(MAX)
DECLARE @body2 NVARCHAR(MAX)
Declare @count int
declare @per int
set @xml=cast(( SELECT [pdate] as 'td','',[Part_Name] AS 'td','',[shift_name] as 'td','',[ptarget] as 'td','',[pruduction] as 'td','',[Achieved] as 'td','',[RFT] as 'td'
FROM  [PCP_Dosser].[dbo].[production_details_all_parts] Where pdate=(select convert( date,GetDate()-1)) and Part_Name='Doser'
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
SET @body ='<html><body><H3>Previous Day Production Details</H3>
<table border=1>
<tr>
<th> Previous Day Doser Line Details</th><.tr>
<th> Date </th> <th>Part Name</th> <th>Shift Name</th><th>Target</th><th>Achieved</th><th>Achieved %</th><th>RFT</th></tr><tr>
</table><br>'


set @xmlI=cast(( SELECT [pdate] as 'td','',[Part_Name] AS 'td','',[shift_name] as 'td','',[ptarget] as 'td','',[pruduction] as 'td','',[Achieved] as 'td','',[RFT] as 'td'
FROM  [PCP_Dosser].[dbo].[production_details_all_parts] Where pdate=(select convert( date,GetDate()-1)) and Part_Name='Injector'
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
SET @body ='<H3>Previous Injector Line Day Production Details</H3>
<table border=2>
<tr>
<th> Date </th> <th>Part Name</th> <th>Shift Name</th><th>Target</th><th>Achieved</th><th>Achieved %</th><th>RFT</th></tr> <tr>
</table><br>'


SET @xmlP = CAST(( SELECT [pdate] as 'td','',[Part_Name] AS 'td','',[shift_name] as 'td','',[ptarget] as 'td','',[pruduction] as 'td','',[Achieved] as 'td','',[RFT] as 'td'
FROM  [PCP_Dosser].[dbo].[production_details_all_parts] Where pdate=(select convert( date,GetDate()-1)) and Part_Name='Pump'
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
SET @body ='<H3>Previous Day Pump Production Updates</H3>
<table border = 3> 
<tr>
<th> Date </th> <th>Part Name</th> <th>Shift Name</th><th>Target</th><th>Achieved</th><th>Achieved %</th><th>RFT</th></tr>'
SET @body = @body+@xml+@xmlI+@xmlP+'</table></body></html>'
--SET @body2 = @body+@xmlI+'</table></body><br/></html>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'NGMES', -- replace with your SQL Database Mail Profile 
@body = @body,
@body_format ='HTML',
@recipients = 'krnmadake04@gmail.com',
--@copy_recipients = @copy_to,
@subject = 'PCP Production Count Update';

1 个答案:

答案 0 :(得分:0)

感谢团队的支持答案查询已解决,感谢您的帮助 对于答案,我正在粘贴脚本,请检查。

DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
declare @paragraph nvarchar(MAX)
DECLARE @para nvarchar(MAX)
Declare @para1 nvarchar(max)
declare @xmlp nvarchar(max)
Declare @xmlI nvarchar(MAX)
Declare @count int
declare  @per int


set @xml=cast(( SELECT [pdate] as 'td','',[Part_Name] AS 'td','',[shift_name] as 'td','',[ptarget] as 'td','',[pruduction] as 'td','',[Achieved] as 'td','',[RFT] as 'td'
FROM  [production_details_all_parts] Where pdate=(select convert( date,GetDate()-1)) and Part_Name='Injector'
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

set @xmlp=cast(( SELECT [pdate] as 'td','',[Part_Name] AS 'td','',[shift_name] as 'td','',[ptarget] as 'td','',[pruduction] as 'td','',[Achieved] as 'td','',[RFT] as 'td'
FROM  [production_details_all_parts] Where pdate=(select convert( date,GetDate()-1)) and Part_Name='Doser'
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

set @xmlI=cast(( SELECT [pdate] as 'td','',[Part_Name] AS 'td','',[shift_name] as 'td','',[ptarget] as 'td','',[pruduction] as 'td','',[Achieved] as 'td','',[RFT] as 'td'
FROM  [production_details_all_parts] Where pdate=(select convert( date,GetDate()-1)) and Part_Name='Pump'
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

SET @body ='<html><body><H3>Previous Day Production Details</H3>
<H4> Injector Production Details</H4>
<table border=1> 
<tr>
<th> Date </th><th>Part Name</th> <th>Shift Name</th> <th> Target </th><th>Achieved</th><th>Achieved %</th><th>RFT</th></tr>'
set @body=@body+@xml+'</table>''<br/>
<H4>Pump Production Details</H4>
<table border=1>
<tr>
<th> Date </th><th>Part Name</th> <th>Shift Name</th> <th> Target </th><th>Achieved</th><th>Achieved %</th><th>RFT</th></tr>'
set @body=@body+@xmlI+'</table>''<br/>
<H4>Doser Production Details</H4>
<table border =1>
<tr>
<th> Date </th> <th>Part Name</th><th>Shift Name</th> <th> Target </th><th>Achieved</th><th>Achieved %</th><th>RFT</th></tr>' 
SET @body = @body+@xmlp+'</table>''<br/>'
set @body=@body+'</body></html>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'NGMES', 
@body = @body,
@body_format ='HTML',
@recipients = 'krnmadake04@gmail.com',
--@copy_recipients = @copy_to,
@subject = 'PCP-Production Count Updates';