[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Database Mail Hyperlink inside of the XML Data From Query

Posted on 2011-03-09
6
Medium Priority
?
1,317 Views
Last Modified: 2012-05-11
I have a query that I want to which sends an email via database mail.  I want to have a hyperlink within the email that the end user can click on to get more information.  I created a view for the data that I want to pull that creates the hyperlink.  The field customer number in the code is the field that outputs the hyperlink. So in the view I have the following -

SELECT
'<a href="http://link.com/Cust/Customer.cfm?CustSearch=' + I.CustomerNumber + '">' + I.CustomerNumber + '</a>' as CustomerNumber
FROM I

The problem is that when I receive the email the hyperlink displays the full hyperlink -
<a href="http://link.com/Cust/Customer.cfm?CustSearch=1234">1234</a>

Just to test it out, I put a hyperlink in the body of an email that was not related to the XML and it worked as expected, where the XML displays the full href tag the link in the body of the email just displays 1234 like I want.  

Is there a way to remedy this?
DECLARE @DiscountOrderbody NVARCHAR(MAX)
DECLARE @DiscountOrderXML NVARCHAR(MAX)

SET @DiscountOrderXml =CAST(
(SELECT 
	td = FirstName,'',
	td = InvoiceNumber,'', 
	td = CustomerNumber,'',
	td = BillToBusinessName,'', 
	td = InventoryNumber,'', 
	td = ModelYear,'', 
	td = ModelName,'', 
	td = WholesalePrice,'',
	td = Quantity,'',
	td = UnitPrice,'', 
	td = v.Discount,'',
	td = AvgExchange,'', 
	td = LookupsTotal,'', 
	td = LookupsInStock,'', 
	td = SoldQty,'', 
	td = ReturnsQty,'',
	td = QtyOnHand,'',
	td = AverageSalePrice,''
FROM dbo.vu_DiscountedParts as V
ORDER BY FirstName, InventoryNumber DESC 
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))



DECLARE @tableHTML nvarchar(max)
SET @tableHTML =
N'<style type="text/css">h2, body {font-family: Arial, verdana;} table{font-size:12px; border-collapse:collapse;} 
td{background-color:#F1F1F1; border:1px solid black; padding:4px;} th{background-color:#99CCFF; padding:4px;}
h4{font-family: "Courier New", Courier, monospace; font-size: 11px;} </style>' 


SET @DiscountOrderbody = 
	@tableHTML + 
	'<H1>Discounted Parts - '  + cast(Month(getdate()) as varchar) + '/' + cast(day(getdate()) as varchar) + '/' + 
	cast(year(getdate()) as varchar) + 
	'</H1>'

	<table border="1" cellspacing="0" style="border: thin solid #000;">
	<tr>
		<th>Salesman</th>
		<th>Invoice</th>
		<th>Cust #</th>
		<th>Cust Name</th>
		<th>Part#</th>
		<th>Year</th>
		<th>Model</th>
		<th>W Price</th>
		<th>Qty</th>
		<th>S Price</th>
		<th>Discount</th>
		<th>Avg Price</th>
		<th>Lookups</th>
		<th>In Stock</th>
		<th>Sold</th>
		<th>Retn</th>
		<th>On Hand</th>
		<th>Avg Price</th>
	</tr>' SET @DiscountOrderbody = @DiscountOrderbody + IsNull(@DiscountOrderXML,'') + '</table>
	<br>
	<img src="http://link.com/link.jpg">
	<br>
	<a href="http://link.com/link.jpg">Click Here!</a>
	</body></html>'
	
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'r@somebody.com',
-- @copy_recipients = 'sales@somebody.com',
@body = @DiscountOrderbody,
@body_format ='HTML',
@subject ='Discounts',
@profile_name ='SchramMail'

Open in new window

0
Comment
Question by:reedsster1
5 Comments
 
LVL 14

Expert Comment

by:Jagdish Devaku
ID: 35286144
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35289287
If you will notice the author is using HTML  Here it is again:
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'r@somebody.com',
-- @copy_recipients = 'sales@somebody.com',
@body = @DiscountOrderbody,
@body_format ='HTML',
@subject ='Discounts',
@profile_name ='SchramMail'
0
 

Accepted Solution

by:
reedsster1 earned 0 total points
ID: 35316873
Fixed.  I just used replace and replaced the reserved xml terms with the HTML that I wanted.  
0
 

Author Closing Comment

by:reedsster1
ID: 35356970
It worked
0
 

Expert Comment

by:OPar79
ID: 35378948
Reedsster1, can you post an example of your replace script? I"m having trouble figuing this out.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

834 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