在 SQL Server 中,我编写了一个脚本,用于从 SQL 数据库发送邮件,我使用 xml 将数据转换为正确的表格格式。现在我希望通过多个查询在邮件中显示多个表。附上一些示例文档以供参考,请找到它并让我知道如何获得所需的输出。还附上了所需的输出示例,请告诉我是否有人找到解决方案
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';