Solved

SQL Database Mail Hyperlink inside of the XML Data From Query

Posted on 2011-03-09
6
1,128 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
[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
6 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

726 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