Solved

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

Posted on 2011-09-12
4
771 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
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…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

13 Experts available now in Live!

Get 1:1 Help Now