Solved

SQL Database Mail Hyperlink inside of the XML Data From Query

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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

749 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