Solved

SQL Database Mail Hyperlink inside of the XML Data From Query

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

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.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

760 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

20 Experts available now in Live!

Get 1:1 Help Now