1

I have a stored procedure that sends daily report for 2 operational machines. I also show on report misfire count per machine but if 1 machine has 0 misfires and the other has more than 0 the Misfire report section will be skipped and not reported.

First I want to check and see if any items exist for specific day and if not send report saying "No results found" or whatever, if they do exist then check to see if any misfires exist on both machines. I want to attach only the stats from the machine that has more than 0 and skip misfire report altogether if they both have 0. I will show what a report looks like that shows Daily Stats that has misfires on both machines.

enter image description here

If say TILT_1 reports 0 misfires and say TILT_2 reports 20 misfires on DAILY SORTER STATS portion then both TILT_1 and TILT_2 MISFIRE REPORT are not attached to report at all. In this instance I would only want to attach TILT_2 MISFIRE REPORT. I hope I am explaining correctly.

Here is the book of a script. Thanks for your help.

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; 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DRUIDRUID
  • 369
  • 1
  • 5
  • 18
  • 1
    This is a large chunk of code. Can you reduce it to the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example https://stackoverflow.com/help/mcve – Greg Viers Feb 09 '18 at 21:48
  • I answered a [related question here:](https://stackoverflow.com/a/48675214/5089204). This uses a function taking any `SELECT`, which will return a fully blown `XHTML` table. You can first insert your queries to temp tables, check your business logic and then use the provided approach to build your final `HTML`. – Shnugo Feb 09 '18 at 21:51

1 Answers1

0

It looks like @Body4 + @Body5 are TILT1, and @Body6 + @Body7 are TILT2

You eventually combine these with:

DECLARE  @BodyAll1  varchar(max)
SET @BodyAll1 = @Body2 + @Body3 + @Body4 + @Body5 + @Body6 + @Body7 

It also looks like this is what gets the misfire count:

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`

If that all is true, you should be able to simply wrap each TILT section in an 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

Then remove your current IF EXISTS()

You could also handle this when you set @BodyAll1, but this way you don't run all of that extra code to create the TILT tables unless you plan to use those tables.

Aaron Dietz
  • 10,137
  • 1
  • 14
  • 26
  • Great I did a combo of both suggestions. Wrapped each TILT misfire section with appropriate `IF() BEGIN.. END` statements and then `SET @BodyAll1 = @Body2 + @Body3 + isnull(@Body4,' ') + isnull(@Body5,' ') + isnull(@Body6,' ') + isnull(@Body7,' ') ` like such. Thanks for help – DRUIDRUID Feb 09 '18 at 23:20
  • Oh, and removed `IF EXISTS()` that looked from misfire count from top of SP – DRUIDRUID Feb 09 '18 at 23:21