Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query results by email using xp_sendmail


query results by email using xp_sendmail

Author
Message
bmr270
bmr270
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 398
Hi

I want to send query results by email using xp_sendmail procedure. I am able to send but the data formatted in wrong way.

How to send query result with proper format?

or

how to attach a FORMATTED query result file in the email?

Any help?

Thanks,
BMR
ESAT ERKEC
ESAT ERKEC
SSC Veteran
SSC Veteran (298 reputation)SSC Veteran (298 reputation)SSC Veteran (298 reputation)SSC Veteran (298 reputation)SSC Veteran (298 reputation)SSC Veteran (298 reputation)SSC Veteran (298 reputation)SSC Veteran (298 reputation)

Group: General Forum Members
Points: 298 Visits: 897
You can use reporting service Subscriptions
grahamc
grahamc
Mr or Mrs. 500
Mr or Mrs. 500 (576 reputation)Mr or Mrs. 500 (576 reputation)Mr or Mrs. 500 (576 reputation)Mr or Mrs. 500 (576 reputation)Mr or Mrs. 500 (576 reputation)Mr or Mrs. 500 (576 reputation)Mr or Mrs. 500 (576 reputation)Mr or Mrs. 500 (576 reputation)

Group: General Forum Members
Points: 576 Visits: 1039
Heres an example of a SELECT statement I use and it comes out nicely formatted :-):


DECLARE @tableHTML VARCHAR(MAX)
DECLARE @tEmailList VARCHAR(MAX)
DECLARE @tProfile VARCHAR(128)
DECLARE @tEmailSubject VARCHAR(2048)

SELECT
@tEmailList = email_address
,@tEmailSubject = CAST(@@SERVERNAME AS VARCHAR(128)) + ' - DB_Admin.dbo.usp_sel_InfoOS'
,@tableHTML = N'<H2>Database Report for Server: '
+ @@SERVERNAME + '</H2>'
+ N'<table border="1">'
+ N'<tr>
<th>Host Name</th>
<th>OS Name</th>'
+ CAST
(
(
SELECT
td = [HostName]
,''
,td = [OSName]
,''
FROM [DB_Admin].[dbo].[tbl_InfoOS]
FOR XML PATH('tr'), TYPE) AS NVARCHAR(MAX))
+ N'</table>
This was generated by the <i>DB_Admin.dbo.usp_sel_InfoOS</i> Stored Procedure
'
FROM msdb.dbo.sysoperators
WHERE [name] = 'DBA'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = @tProfile
,@recipients = @tEmailList
,@subject = @tEmailSubject
,@body_format = 'html'
,@body = @tableHTML



bmr270
bmr270
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 398
Thanks for you both.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search