База данных SQL Почтовая служба - PullRequest
0 голосов
/ 20 марта 2019

В 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 голосов
/ 20 марта 2019

Спасибо Команда получила Ответ на вопрос решен спасибо за вашу помощь для ответа я вставляю скрипт, пожалуйста, проверьте.

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';
...