-1

The issue I am encountering is when the email is sent to me, the results sent to my email is not formatted correctly in a html table.

First, here are my queries. The first query builds a temp table with the data that contains type M and C. The second query builds another temp table and compares the temp table created before and checks if the data contains type M or C.

use Letters

-- Build a temp table with the M and C Denial letters. We do not want to include these in our final list.

select distinct
LT.Description
into #ModCarvLetters
from dbo.AELetters_LetterTemplateText TT (nolock)
inner join dbo.AELetters_LetterTemplates LT (nolock) on 
TT.TemplateUniqueID = LT.TemplateUniqueID
inner join dbo.AELetters_LetterAssignment LA (nolock) on
LT.TemplateUniqueID = LA.TemplateUniqueID
inner join dbo.AELetters_LetterAssignmentDetail LAD (nolock) on
LA.LetterAssignmentID = LAD.LetterAssignmentID
where CurrentVersion = 1
and LT.TemplateType = 'Auto'
and LAD.LetterAssignment_lkupID = 3 --- Autst
and (LAD.MatchTableColumnValue =  'C' or LAD.MatchTableColumnValue =  'M')
and cast(LA.EffFrom as DATE) <= cast(GETDATE() as DATE)
and cast(LA.EffTo as DATE) >= cast(GETDATE() as DATE)
order by LT.Description

-- Get the language from the letter just before the denial reason.

select distinct
LT.Description,
substring(TT.TemplateText, charindex('Denial', TT.TemplateText)-500, 500) as [Denial_Reason]
into #DenialLetters
from dbo.AELetters_LetterTemplateText TT (nolock)
inner join dbo.AELetters_LetterTemplates LT (nolock) on 
TT.TemplateUniqueID = LT.TemplateUniqueID
inner join dbo.AELetters_LetterAssignment LA (nolock) on
LT.TemplateUniqueID = LA.TemplateUniqueID
inner join dbo.AELetters_LetterAssignmentDetail LAD (nolock) on
LA.LetterAssignmentID = LAD.LetterAssignmentID
where CurrentVersion = 1
and LT.TemplateType = 'Auto'
and TT.LanguageCodeID  = 65 -- English
--and LAD.LetterAssignment_lkupID = 2
and LAD.MatchTableColumnValue = 'Denied'
and cast(LA.EffFrom as DATE) <= cast(GETDATE() as DATE)
and cast(LA.EffTo as DATE) >= cast(GETDATE() as DATE)
and LT.Description not in (select M.Description from  #ModCarvLetters M)
order by LT.Description desc--, TT.TemplateText

drop  table #ModCarvLetters 

The following is where I send the results an a email.

DECLARE @CT AS INT
DECLARE @LETTER_DESCRIPTION AS VARCHAR (255)
DECLARE @DENIAL_REASON AS VARCHAR (MAX)
DECLARE @EMESSAGE AS VARCHAR (MAX)

SELECT @CT = COUNT(*) FROM  #DenialLetters

                        SET @EMESSAGE = '<head><style type="text/css">h2 {font-family: Arial, verdana;} td{background-color:#F1F1F1; border:1px solid black; padding:3px;} th{background-color:#99CCFF;}</style>
                        <h2><font color="#0000ff" size="4">DENIAL LETTERS ON TEST</font></h2></head><table border="1"><tr><th>Letter Description</th><th>Denial Reason</th></tr>';
                        --<h2><font color="#0000ff" size="4">DENIAL LETTERS ON PRODUCTION</font></h2></head><table border="1">' + '<tr><th>Letter Description</th><th>Denial Reason</th></tr>';

                        WHILE @CT > 0

                        BEGIN

                              SELECT @LETTER_DESCRIPTION = Description, @DENIAL_REASON = Denial_Reason  FROM  #DenialLetters

                              SET @EMESSAGE = @EMESSAGE + 
                              '<tr><td>' + @LETTER_DESCRIPTION + '</td><td>' +  @DENIAL_REASON + '</td></tr>' ;

                              DELETE FROM  #DenialLetters WHERE (Description = @LETTER_DESCRIPTION and Denial_Reason = @DENIAL_REASON)

                              SELECT @CT = COUNT(*) FROM  #DenialLetters
                              --Print @CT
                              --Print @LETTER_DESCRIPTION
                              --Print  @DENIAL_REASON
                        END

                        SET @EMESSAGE = @EMESSAGE + '</table>'

                        exec msdb.dbo.sp_send_dbmail      
                             @recipients = 'mbruhn@regalmed.com',
                             @subject = 'Denial letters on REA Test',
                           --@subject = 'Denial letters on REA Production',  
                             @body = @EMESSAGE,
                             @body_format = 'HTML'              

Drop table  #DenialLetters

And here is what the results appear when I recieve the email. The problem I am encountering is that the text outside of the table should be in the table. The "!=""){%>" shouldnt appear.

enter image description here

Roberto Flores
  • 775
  • 2
  • 12
  • 46
  • I assume you mean the bit of text `!=""){ %>` is the problem. Is everything else acceptable? You didn't actually say what you believed to be wrong with the email. We don't know your requirements. Anyway to me it looks like it might be something in the TT.TemplateText field might plausibly be the cause. It doesn't look like it can be coming from the HTML generating code you've shown us. – ADyson Aug 31 '18 at 20:35
  • @ADyson: I have updated my question. Regarding the TT.TemplateText field that might be causing it, how would I resolve that – Roberto Flores Aug 31 '18 at 20:58
  • I don't know. I don't know what's in that field and I don't know what's supposed to be in it either. Nor do I know how it gets populated. Presumably you have some idea about it, or you have a way to find out. First you need to verify that that's really the issue, and then if so you'll have to make a decision about whether to remove the offending content. I have no way to decide that for you really. – ADyson Aug 31 '18 at 21:01
  • @ADyson: The TT.TemplateText contains text and codeblocks. The bit of text you mentioned above is part of the codeblock that did not display in the table. The rest of the content below the table should also be in the table, in their own TD's. – Roberto Flores Aug 31 '18 at 21:29
  • Ok. I notice in your SQL you're trying to select only part of the field. Perhaps you're selecting the wrong bit. It's hard to know without seeing the field contents for the row in question. – ADyson Aug 31 '18 at 21:37
  • @ADyson: You are right about selecting only part of the field. I will look into that. Do you happen to know, like in my query, a way for me to remove specific characters like "{}[!=><%]." I think those characters are causing the weird html output I am seeing – Roberto Flores Aug 31 '18 at 21:45
  • they are. Should be assume those values are in the field correctly? Or does the data source need cleaning? Assuming the table data is correct, then...either select a different number of characters from your field so it doesn't overlap that section, or if that's not possible then use a function to alter the string selected by the query - T-SQL has lots of string functions, including for replacing or removing values from a string. Google it and you'll find the docs easily. – ADyson Aug 31 '18 at 21:47
  • Instead of creating your XML on string level (concatenation) you might [want to read this answer](https://stackoverflow.com/a/39487565/5089204). XML on string level can lead into various hardly to find issues... – Shnugo Sep 01 '18 at 10:37
  • A general hint: `SELECT 'a&<>€Whatever' AS [*] FOR XML PATH('')` will translate any string to an XML-save format. Another option was to place your text as `CDATA` section. – Shnugo Sep 01 '18 at 10:40

1 Answers1

0

Instead of using the loop, you could try this:

SET @EMESSAGE = @EMESSAGE + 
    (SELECT Description td, Denial_Reason td
     FROM #DenialLetters
     FOR XML RAW ('tr'), ELEMENTS)
Wolfgang Kais
  • 4,010
  • 2
  • 10
  • 17