Question

I am working on an email alert in trigger where body text has to be styled like this:

Counterparty: a

Previous Status: (Any status should be red)

New Status:  TRDNG OK(Green colored and Bold Upper)

Any help is appreciated. Thanks.

Was it helpful?

Solution

You can specify HTML format when sending the email by using the @body_format parameter. You will need to build a HTML formatted message to set as the @body, something similar to the below:

DECLARE @counterparty NVARCHAR(50)
      , @previous_status NVARCHAR(50)
      , @new_status NVARCHAR(50);

-- 
-- (EXECUTE WHATEVER QUERY SETS THESE VALUES HERE)
--

-- ONCE VALUES ARE SET
DECLARE @html NVARCHAR(MAX) = N'
<div>
    <label for="counterparts">
        Counterparty:
    </label>
    <span id="counterparty">' +
        @counterparty + N'
    </span>
    <br/>
    <label for="previous status">
        Previous Status:
    </label>
    <span id="previous status" style="color: red;">' + 
        @previous_status + N'
    </span>
    <br/>
    <label for="new status">
        New Status:
    </label>
    <span id="new status" style="color: green; font-weight: bold;">' +
        UPPER(@new_status) + N'
    </span>
</div>';

EXEC msdb.dbo.sp_send_dbmail @recipients='recipients@mycompany.com',  
@subject = 'Title',  
@body = @html,  
@body_format = 'HTML' ; 

You can find more information in the documentation for sp_send_dbmail and if you really want to invest time in making the emails visually appealing, you may also want to check out some examples of HTML/CSS.

EDIT

Based on the comment below, you indicate that you want to change the colour of the text based upon which status is set. This can be achieved easily by using the CASE statement to assign the colour:

'<span id="new status" style="color: ' 
     + CASE @new_status
         WHEN 'a' 
           THEN 'green'
           ELSE 'red'
        END + '; font-weight: bold;">' +
     UPPER(@new_status) + N'
 </span>'
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top