?
Solved

Formatting a clickable link in the body of a message using sp_send_dbmail

Posted on 2011-09-12
4
Medium Priority
?
980 Views
Last Modified: 2012-05-12
I am trying to send a message using sp_send_dbmail that has several fields, and one field that I would like to make clickable. This is the code I am currently using that I would have thought would work.

select td = '<a href="http://www.somewebsite.com">Login<a/>' + reportnumber from sometable for xml PATH('tr')

The problem is the link works fine, but is displayed as "http://www.somewebsite.com" instead of just "Login" as the code would suggest. The actual link I am using is far longer than that making the report unreadable unless I can shorten it down to just "Login" as it should act.

I do have the @body_format = 'HTML' set correctly...
0
Comment
Question by:bccops
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 1500 total points
ID: 36526833
If you look at the output you will see the the FOR XML is encoding your output as follows:
<tr>
  <td>&lt;a href="http://www.somewebsite.com"&gt;Login&lt;a/&gt;somereportnumber</td>
</tr>

Which I am sure is not what you intended.
0
 

Author Comment

by:bccops
ID: 36531901
I have included the whole code sample to maybe this easier.
DECLARE @tableHTML  NVARCHAR(MAX) ;

SET @tableHTML =
    '<H1>Response Time Report (Greater than 12 mins as test)</H1>' +
    '<table border="1">' +
    '<tr><th>AR</th><th>Time Created</th>' +
    '<th>Dispatched</th><th>Enroute</th><th>Arrival</th>' +
    '<th>Turnout</th><th>Travel</th><th>Address</th></tr>' +
    CAST ( ( SELECT td = '<a href="http://msdn.microsoft.com/en-us/library/bb545450.aspx">Login</a>' + right(reportnumber,5), '',
                    td = timecreate, '',
                    td = timedispatched, '',
                    td = timeenroute, '',
                    td = timearrive, '',
					td = datediff(ss,timedispatched,timeenroute), '',
					td = datediff(ss,timeenroute, timearrive), '',
                    td = address
              FROM cc_unittimes_ems
              WHERE timeenroute is not null
and timearrive is not null			
and datediff(ss, timecreate, timearrive) > 1200
and datediff(hh, timecreate, getdate()) < 224
				order by right(reportnumber,5) asc
                FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX) ) +
    N'</table>' ;

EXEC msdb.dbo.sp_send_dbmail 
	@profile_name = 'Main',
	@recipients='nospam@email.com',
    @subject = 'Long Responses',
    @body = @tableHTML,
    @body_format = 'HTML' ;

Open in new window

0
 

Author Comment

by:bccops
ID: 36537166
While I didnt catch it at first. I was given the answer in a roundabout way. The total solution was to add the following as a result of the improper format.

set @tableHTML = REPLACE( @tableHTML, '&lt;', '<' );
set @tableHTML = REPLACE( @tableHTML, '&gt;', '>' );
set @tableHTML = REPLACE( @tableHTML, '&amp;', '&' );

0
 

Author Closing Comment

by:bccops
ID: 36537178
Complete solution was to add

set @tableHTML = REPLACE( @tableHTML, '&lt;', '<' );
set @tableHTML = REPLACE( @tableHTML, '&gt;', '>' );
set @tableHTML = REPLACE( @tableHTML, '&amp;', '&' );
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question