November 13, 2008 at 6:11 am
Hi,
I want to send the results of a query as the message of an SMTP mail i.e.
@message = SELECT * FROM #temp
of course this doesn't work. I have tried BOL but can't seem to find anything relevant.
Is there a simple way that I am missing?
November 13, 2008 at 7:07 am
Use sp_send_dbmail. There is the following parameter.
Per Bol:
@query = ] 'query'
Is a query to execute. The results of the query can be attached as a file, or included in the body of the e-mail message. The query is of type nvarchar(max), and can contain any valid Transact-SQL statements. Note that the query is executed in a separate session, so local variables in the script calling sp_send_dbmail are not available to the query.
November 14, 2008 at 2:07 am
Thanks,
Iam still mentally upgrading from 2000 - 2005 and was still trying to use SMTP mail. Configured the query with sp_send_dbmail and it works perfectly.
November 14, 2008 at 7:25 pm
Denis Wilkinson (11/14/2008)
Thanks,Iam still mentally upgrading from 2000 - 2005 and was still trying to use SMTP mail. Configured the query with sp_send_dbmail and it works perfectly.
Any chance of seeing that fine query... I'm getting ready to do something similar. Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 17, 2008 at 3:16 am
Fairly simple Jeff, nothing special, the only issue was finding the @profile_name, I found this link which explained it all http://msdn.microsoft.com/en-us/library/ms174410.aspx
Best of luck.
--Send file embedded in the body of the email
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Newlands Auto Email',
@recipients = 'denis.wilkinson@za.sabmiller.com; constant.witbooi@za.sabmiller.com; stuart.yates@za.sabmiller.com',
@body = 'Please maximise width of the email to see all the columns correctly',
@query = 'select * from NEWSS160.eQMSDiagnostics.dbo.Tagtemp' ,
@query_result_width = '250',
@subject = 'InSQL EQMS Tags in Pipe-Reconnect',
@attach_query_result_as_file = 0 ;
November 17, 2008 at 3:03 pm
--Any chance of seeing that fine query... I'm getting ready to do something similar. Thanks.
--Jeff Moden.
Hi Jeff
I implemented smtp mail (SQL 2000) and dbmail (SQL 2005) a few months ago,
below is a sample of 2 methods you can use after configuring db mail in SQL 2005:
1. using a stored procedure (usp_Create_Entry):
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'sqlservacct',
@recipients = 'kevin@hackers.com',
@query = 'EXEC mydatabase.dbo.usp_Create_Entry' ,
@subject = 'File FTP notification' ;
2. using a select statement:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'sqlservacct',
@recipients = 'kevin@hackers.com',
@query = 'SELECT COUNT(*) FROM AdventureWorks.Production.WorkOrder
WHERE DueDate > ''2008-08-08'' ',
@subject = 'Production Work Order' ;
Regards
Kevin
November 17, 2008 at 5:27 pm
Cool... thanks, guys.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 18, 2008 at 1:23 pm
Jeff,
You can also specify a proc/view name for your query. Such as:
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'yourname@yourcompany.com',
@subject = 'Proc Test',
@body = 'Proc Test Results:',
@query = 'Yourdbname..yourProcOrViewName',
@importance = 'High'
Or a Proc With Parameter(s) if needed:
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'yourname@yourcompany.com',
@subject = 'Proc Test With Parameter Specified',
@body = 'Proc With Parameter Test Results:',
@query = 'Yourdbname..yourprocname ''yourparameter''',
@importance = 'High'
Sorry! Already mentioned above!
November 18, 2008 at 5:45 pm
James A. Lawrence (11/18/2008)
Sorry! Already mentioned above!
Still, thanks for the thought.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 19, 2008 at 8:27 am
I do send the attachment all the time using the following script-
----(you need to configure the db mail and create a profile)
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DBA Notification',
@recipients = 'yourname@yourcompany.com',
@subject = 'whatever Report',
@body = Report name:',
@query = 'Yourdbname..yourprocname ''yourparameter''',
@body_format = 'TEXT',
@attach_query_result_as_file = 1,
@query_attachment_filename ='Report.CSV',
@query_result_header = 1,
@query_result_width = 32767,
@query_result_separator = " "
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply