?
Solved

SQL Database Mail Hyperlink inside of the XML Data From Query

Posted on 2011-03-09
6
Medium Priority
?
1,191 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

764 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