Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I want to send an Email from Database Mail, but it is first time for me to deal with it,

i am try to do like that:



SQL
DECLARE @tableHTML  NVARCHAR(MAX) ;

SET @tableHTML =
    (N'<h1>Work Order Report</h1>' +
    N'<table border="1">' +
    N'<tr><th>Work Order ID</th><th>Product ID</th>' +
    N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
    N'<th>Expected Revenue</th></tr>' +
    CAST (( select Users.UserName from Users ) AS NVARCHAR(MAX) ) +
    N'</table>') ;

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'myProfile',
    @recipients = 'myEmail@hotmail.com',
    @subject = 'SQL Report',
    @body_format = 'HTML', 
	@execute_query_database = 'DataBaseName',   
	@body = @tableHTML;





The email is send successfully but with no body and it give me error that:



Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

also i want to data that selected from Database to be displayed in body whereas every row is located in separate line in body



any reply will be appreciated

Thanks
Posted
Updated 4-Dec-11 14:43pm
v2
Comments
D K N T H 4-Dec-11 20:37pm    
why do you want to do that, may i know what do you want to do?, so that we can have workaround
MrLonely_2 4-Dec-11 20:44pm    
Thanks for your replay,
I updated my question

1 solution

You have to loop through the table which you are getting and have to create HTMl for the mail..and after creating HTML string you can simly send the mail using bellow code.

EXEC sp_send_dbmail
     @profile_name = 'MailProfile1',
     @recipients = 'someone@microsoft.com',
     @subject = 'HTML Format',
     @body_format = 'HTML',
     @body = '<html><body><p>An html link to www.bing.com is embedded in this email. The link is here: <a href="http://www.bing.com">u bing?</a></p></body></html>'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900