我有一个存储过程,可以为2台操作机器发送每日报告。我还会在每台计算机上显示报告misfire count
,但如果1台计算机发生0
次失火,而另一台计算机发生0
次以上,则会跳过“失火报告”部分,但不会报告。
首先,我想查看特定日期是否存在任何items
,如果没有发送报告说“找不到结果”或其他任何内容,如果确实存在,请检查是否有misfires
存在于两台机器上。我想只附加超过0
的机器中的统计信息,如果它们都有0
,则完全跳过失败报告。我将显示报告的内容,显示两台计算机上都有misfires
的每日统计信息。
如果说TILT_1
报告0
失误,并且TILT_2
报告20
在每日分拣报告部分失误,那么TILT_1和TILT_2 MISFIRE报告都不会附在报告上。在这种情况下,我只想附加TILT_2 MISFIRE REPORT。我希望我能正确解释。
这是一本剧本。谢谢你的帮助。
DECLARE
@Now DATETIME ,
@Monday DATETIME ,
@Friday DATETIME ,
@StartTime VARCHAR(16) ,
@EndTime VARCHAR(16) ,
@Message VARCHAR (50),
@FileName VARCHAR (50),
@Final VARCHAR (50)
SET @StartTime = '12:01:00AM'
SET @EndTime = '11:59:59PM'
SET @Now = GETDATE()
SET @Monday = DATEADD(dd, DATEDIFF(dd, 0, @Now), -0)
SET @Friday = DATEADD(dd, DATEDIFF(dd, 0, @Now), -0)
SET @Message = 'No results found for'
SET @FileName = CONVERT(varchar(30), GETDATE(), 107) + ' Sorter Stats 17P'
SET @Final = CAST(@message as varchar(70)) + ' ' + CAST(DATENAME (WEEKDAY, @Now)AS VARCHAR (30)) + ',' + ' ' + CONVERT(VARCHAR(30), @Now, 107)
-----------------------------------------------------------------
-----------------------------------------------------------------
IF EXISTS(
SELECT *
FROM [dbo].[StatData]
WHERE CreationTime BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID IN ('TILT_1', 'TILT_2')
HAVING FLOOR(SUM(Throws)/2) = 0
)
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'My_profile',
@recipients = 'whatever@yep.com' ,
@subject = @FileName,
@importance = 'High',
@body = @Final,
@query_result_separator = ' ';
RETURN
END
-----------------------------------------------------------------
-----------------------------------------------------------------
ELSE
IF EXISTS (
SELECT 1
FROM [dbo].[MisfireLog]
WHERE RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND WorkstationID IN ('T01', 'T02')
HAVING COUNT (MisfireOrder) = 0
)
BEGIN
--------------------START SORTER STATS.NO MISFIRE INFO-----------
-- Email Query--
DECLARE @Body varchar(max)
declare @TableHead varchar(max)
declare @TableTail varchar(max)
declare @mailitem_id as int
declare @statusMsg as varchar(max)
declare @Error as varchar(max)
declare @Note as varchar(max)
Set NoCount On;
set @mailitem_id = null
set @statusMsg = null
set @Error = null
set @Note = null
Set @TableTail = '</table></body></html>';
--HTML layout--
Set @TableHead =
'<html><head>' +
'<H1 style="color: #000000">Daily Sorter Stats</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Sorter</b></td>' +
'<td align=center><b>Items</b></td>' +
'<td align=center><b>Misfire</b></td></tr>';
--Select information for the Report--
Select @Body= (Select
[TD] = t1.Sorter,
[TD] = t1.Items,
[TD] = t2.Misfire
FROM
(
SELECT Items = floor(sum(Throws)/2)
,Sorter = 'TILT_1'
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_1'
)t1
CROSS JOIN
(
SELECT Misfire = isnull(FLOOR(sum(misfire)),0)
,'TILT_1' AS Sorter
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_1'
)t2
CROSS JOIN
(
SELECT DisabledTrays = isnull(COUNT(SorterID),0)
,'TILT_1' AS Sorter
FROM [dbo].[DisabledCarriers]
WHERE SorterID = 'TILT_1'
)t3
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body = Replace(@Body, '_x0020_', space(1))
Set @Body = Replace(@Body, '_x003D_', '=')
Set @Body = Replace(@Body, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body = Replace(@Body, '<TRRow>0</TRRow>', '')
Set @Body = @TableHead + @Body + @TableTail
-- return output--
Select @Body
-----------------------------------------------------------------
-----------------------------------------------------------------
-- Email Query--
DECLARE @Body1 varchar(max)
declare @TableHead1 varchar(max)
declare @TableTail1 varchar(max)
declare @mailitem_id1 as int
declare @statusMsg1 as varchar(max)
declare @Error1 as varchar(max)
declare @Note1 as varchar(max)
Set NoCount On;
set @mailitem_id1 = null
set @statusMsg1 = null
set @Error1 = null
set @Note1 = null
Set @TableTail1 = '</table></body></html>';
--HTML layout--
Set @TableHead1 =
'<html><head>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Sorter</b></td>' +
'<td align=center><b>Items</b></td>' +
'<td align=center><b>Misfire</b></td></tr>';
--Select information for the Report--
Select @Body1= (Select
[TD] = t1.Sorter,
[TD] = t1.Items,
[TD] = t2.Misfire
FROM
(
SELECT Items = isnull(floor(sum(Throws)/2),0)
,'TILT_2' AS Sorter
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_2'
)t1
CROSS JOIN
(
SELECT Misfire = isnull(FLOOR(sum(misfire)),0)
, 'TILT_2' AS Sorter
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_2'
)t2
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body1 = Replace(@Body1, '_x0020_', space(1))
Set @Body1 = Replace(@Body1, '_x003D_', '=')
Set @Body1 = Replace(@Body1, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body1 = Replace(@Body1, '<TRRow>0</TRRow>', '')
Set @Body1 = @TableHead1 + @Body1 + @TableTail1
-- return output--
Select @Body1
-----------------------------------------------------------------
-----------------------------------------------------------------
--Email
DECLARE @BodyAll varchar(max)
SET @BodyAll = @Body + @Body1
EXEC msdb.dbo.sp_send_dbmail
@profile_name ='My_profile',
@recipients = 'whatever@yep.com',
@subject = @FileName,
@body = @BodyAll,
@body_format = 'HTML'
RETURN
END
ELSE
BEGIN
-----------------------------------------------------------------
-----------------------------------------------------------------
--------------------------------------ALL STATS.WITH MISFIRES----
-----------------------------------------------------------------
-----------------------------------------------------------------
-- Email Query--
DECLARE @Body2 varchar(max)
declare @TableHead2 varchar(max)
declare @TableTail2 varchar(max)
declare @mailitem_id2 as int
declare @statusMsg2 as varchar(max)
declare @Error2 as varchar(max)
declare @Note2 as varchar(max)
Set NoCount On;
set @mailitem_id2 = null
set @statusMsg2 = null
set @Error2 = null
set @Note2 = null
Set @TableTail2 = '</table></body></html>';
--HTML layout--
Set @TableHead2 =
'<html><head>' +
'<H1 style="color: #000000">Daily Sorter Stats</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Sorter</b></td>' +
'<td align=center><b>Items</b></td>' +
'<td align=center><b>Misfire</b></td></tr>';
--Select information for the Report--
Select @Body2= (Select
[TD] = t1.Sorter,
[TD] = t1.Items,
[TD] = t2.Misfire
FROM
(
SELECT Items = floor(sum(Throws)/2)
,Sorter = 'TILT_1'
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_1'
)t1
CROSS JOIN
(
SELECT Misfire = isnull(FLOOR(sum(misfire)),0)
,'TILT_1' AS Sorter
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_1'
)t2
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body2 = Replace(@Body2, '_x0020_', space(1))
Set @Body2 = Replace(@Body2, '_x003D_', '=')
Set @Body2 = Replace(@Body2, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body2 = Replace(@Body2, '<TRRow>0</TRRow>', '')
Set @Body2 = @TableHead2 + @Body2 + @TableTail2
-- return output--
Select @Body2
-----------------------------------------------------------------
-----------------------------------------------------------------
-- Email Query--
DECLARE @Body3 varchar(max)
declare @TableHead3 varchar(max)
declare @TableTail3 varchar(max)
declare @mailitem_id3 as int
declare @statusMsg3 as varchar(max)
declare @Error3 as varchar(max)
declare @Note3 as varchar(max)
Set NoCount On;
set @mailitem_id3 = null
set @statusMsg3 = null
set @Error3= null
set @Note3 = null
Set @TableTail3 = '</table></body></html>';
--HTML layout--
Set @TableHead3 =
'<html><head>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Sorter</b></td>' +
'<td align=center><b>Items</b></td>' +
'<td align=center><b>Misfire</b></td></tr>';
--Select information for the Report--
Select @Body3= (Select
[TD] = t1.Sorter,
[TD] = t1.Items,
[TD] = t2.Misfire
FROM
(
SELECT Items = isnull(floor(sum(Throws)/2),0)
,'TILT_2' AS Sorter
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_2'
)t1
CROSS JOIN
(
SELECT Misfire = isnull(FLOOR(sum(misfire)),0)
, 'TILT_2' AS Sorter
FROM [dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_2'
)t2
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body3 = Replace(@Body3, '_x0020_', space(1))
Set @Body3 = Replace(@Body3, '_x003D_', '=')
Set @Body3 = Replace(@Body3, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body3 = Replace(@Body3, '<TRRow>0</TRRow>', '')
Set @Body3 = @TableHead3 + @Body3 + @TableTail3
-- return output--
Select @Body3
-------------------------------------INSERT MISFIRE STATS HERE---
-- Email Query--
DECLARE @Body4 varchar(max)
declare @TableHead4 varchar(max)
declare @TableTail4 varchar(max)
declare @mailitem_id4 as int
declare @statusMsg4 as varchar(max)
declare @Error4 as varchar(max)
declare @Note4 as varchar(max)
Set NoCount On;
set @mailitem_id4 = null
set @statusMsg4 = null
set @Error4 = null
set @Note4 = null
Set @TableTail4 = '</table></body></html>';
--HTML layout--
Set @TableHead4 = '<html><head>' +
'<H1 style="color: #000000">Tilt 1 Misfire Report</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Misfire Count</b></td>' +
'<td align=center><b>Chute ID</b></td></tr>';
--Select information for the Report--
Select @Body4= (Select
[TD] = isnull(COUNT (MisfireOrder),0),
[TD] = ChuteID
from [dbo].[MisfireLog]
where RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND WorkstationID = 'T01'
group by ChuteID
order by COUNT (MisfireOrder) desc
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body4 = Replace(@Body4, '_x0020_', space(1))
Set @Body4 = Replace(@Body4, '_x003D_', '=')
Set @Body4 = Replace(@Body4, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body4 = Replace(@Body4, '<TRRow>0</TRRow>', '')
Set @Body4 = @TableHead4 + @Body4 + @TableTail4
-- return output--
Select @Body4
-----------------------------------------------------------------
-----------------------------------------------------------------
-- Email Query--
DECLARE @Body5 varchar(max)
declare @TableHead5 varchar(max)
declare @TableTail5 varchar(max)
declare @mailitem_id5 as int
declare @statusMsg5 as varchar(max)
declare @Error5 as varchar(max)
declare @Note5 as varchar(max)
Set NoCount On;
set @mailitem_id5 = null
set @statusMsg5 = null
set @Error5 = null
set @Note5 = null
Set @TableTail5 = '</table></body></html>';
--HTML layout--
Set @TableHead5 = '<html><head>' +
--'<H1 style="color: #000000">Tilt 2 Misfire Report</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Misfire Count</b></td>' +
'<td align=center><b>Carrier ID</b></td></tr>';
--Select information for the Report--
Select @Body5= (Select
[TD] = isnull(COUNT (MisfireOrder),0),
[TD] = CarrierID
from [dbo].[MisfireLog]
where RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND WorkstationID = 'T01'
group by CarrierID
order by COUNT (MisfireOrder) desc
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body5 = Replace(@Body5, '_x0020_', space(1))
Set @Body5 = Replace(@Body5, '_x003D_', '=')
Set @Body5 = Replace(@Body5, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body5 = Replace(@Body5, '<TRRow>0</TRRow>', '')
Set @Body5 = @TableHead5 + @Body5 + @TableTail5
-- return output--
Select @Body5
-----------------------------------------------------------------
-----------------------------------------------------------------
-- Email Query--
DECLARE @Body6 varchar(max)
declare @TableHead6 varchar(max)
declare @TableTail6 varchar(max)
declare @mailitem_id6 as int
declare @statusMsg6 as varchar(max)
declare @Error6 as varchar(max)
declare @Note6 as varchar(max)
Set NoCount On;
set @mailitem_id6 = null
set @statusMsg6 = null
set @Error6 = null
set @Note6 = null
Set @TableTail6 = '</table></body></html>';
--HTML layout--
Set @TableHead6= '<html><head>' +
'<H1 style="color: #000000">Tilt 2 Misfire Report</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Misfire Count</b></td>' +
'<td align=center><b>Chute ID</b></td></tr>';
--Select information for the Report--
Select @Body6= (Select
[TD] = isnull(COUNT (MisfireOrder),0),
[TD] = ChuteID
from [dbo].[MisfireLog]
where RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND WorkstationID = 'T02'
group by ChuteID
order by COUNT (MisfireOrder) desc
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body6 = Replace(@Body6, '_x0020_', space(1))
Set @Body6 = Replace(@Body6, '_x003D_', '=')
Set @Body6 = Replace(@Body6, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body6 = Replace(@Body6, '<TRRow>0</TRRow>', '')
Set @Body6 = @TableHead6 + @Body6 + @TableTail6
-- return output--
Select @Body6
-----------------------------------------------------------------
-----------------------------------------------------------------
-- Email Query--
DECLARE @Body7 varchar(max)
declare @TableHead7 varchar(max)
declare @TableTail7 varchar(max)
declare @mailitem_id7 as int
declare @statusMsg7 as varchar(max)
declare @Error7 as varchar(max)
declare @Note7 as varchar(max)
Set NoCount On;
set @mailitem_id7 = null
set @statusMsg7 = null
set @Error7 = null
set @Note7 = null
Set @TableTail7 = '</table></body></html>';
--HTML layout--
Set @TableHead7 = '<html><head>' +
--'<H1 style="color: #000000">Tilt 2 Misfire Report</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Misfire Count</b></td>' +
'<td align=center><b>Carrier ID</b></td></tr>';
--Select information for the Report--
Select @Body7= (Select
[TD] = isnull(COUNT (MisfireOrder),0),
[TD] = CarrierID
from [dbo].[MisfireLog]
where RecordedPeriod BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND WorkstationID = 'T02'
group by CarrierID
order by COUNT (MisfireOrder) desc
For XML raw('tr'), Elements)
-- Replace the entity codes and row numbers
Set @Body7 = Replace(@Body7, '_x0020_', space(1))
Set @Body7 = Replace(@Body7, '_x003D_', '=')
Set @Body7 = Replace(@Body7, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body7 = Replace(@Body7, '<TRRow>0</TRRow>', '')
Set @Body7 = @TableHead7 + @Body7 + @TableTail7
-- return output--
Select @Body7
-----------------------------------------------------------------
-----------------------------------------------------------------
--Email
DECLARE @BodyAll1 varchar(max)
SET @BodyAll1 = @Body2 + @Body3 + @Body4 + @Body5 + @Body6 + @Body7
EXEC msdb.dbo.sp_send_dbmail
@profile_name ='My_profile',
@recipients = 'whatever@yep.com',
@subject = @FileName,
@body = @BodyAll1,
@body_format = 'HTML'
END;
答案 0 :(得分:0)
看起来@Body4 + @Body5
是TILT1,@Body6 + @Body7
是TILT2
你最终将这些结合起来:
DECLARE @BodyAll1 varchar(max)
SET @BodyAll1 = @Body2 + @Body3 + @Body4 + @Body5 + @Body6 + @Body7
看起来这也是失火计数的原因:
SELECT Misfire = isnull(FLOOR(sum(misfire)),0)
, 'TILT_1' AS Sorter --or `TILT_2`
FROM [PASTEUR].[WSS].[dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_1' --or `TILT_2`
如果一切正确,您应该能够简单地将每个TILT部分包装在IF() BEGIN.. END
中:
IF (SELECT ISNULL(FLOOR,SUM(MISFIRE)),0)
FROM [PASTEUR].[WSS].[dbo].[StatData]
WHERE HourBlock BETWEEN @Monday + ' ' + @StartTime
AND @Friday + ' ' + @EndTime
AND SorterID = 'TILT_1') > 0
BEGIN
--All of @Body4 and @Body5 code
END
然后删除您当前的IF EXISTS()
您可以在设置@BodyAll1
时处理此问题,但这样您就不会运行所有额外的代码来创建TILT表,除非您打算使用这些表。