Solved

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

Posted on 2011-09-12
4
789 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
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dataset not reading table data 12 46
SQL Date from a string 4 57
What is the best way to calculate hours worked 5 61
Need help debbuging stored procedure 21 33
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…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now