[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-09-12
4
Medium Priority
?
1,022 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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