From: Dan on
Is there an easy way to email the results of a query using sp_send_dbmail? I
can always use a cursor and build a string for the email body, but I was
hoping there would be a better way.


From: Plamen Ratchev on
Take a look at the @query parameter.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
From: SQL Menace on
From Books On Line sp_send_dbmail (Transact-SQL) Example B

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks Administrator',
@recipients = 'danw(a)Adventure-Works.com',
@query = 'SELECT COUNT(*) FROM AdventureWorks.Production.WorkOrder
WHERE DueDate > ''2004-04-30''
AND DATEDIFF(dd, ''2004-04-30'', DueDate) < 2' ,
@subject = 'Work Order Count',
@attach_query_result_as_file = 1 ;


Denis The SQL Menace
http://www.lessthandot.com/
http://sqlservercode.blogspot.com
http://sqlblog.com/blogs/denis_gobo/default.aspx



On Jul 15, 4:52 pm, "Dan" <dantheri...(a)hotmail.com> wrote:
> Is there an easy way to email the results of a query using sp_send_dbmail? I
> can always use a cursor and build a string for the email body, but I was
> hoping there would be a better way.

From: Dan on
Perfect. Can't belive I missed that. Thanks.


"SQL Menace" <denis.gobo(a)gmail.com> wrote in message
news:6f1422fc-b7ee-4787-a282-0b823110fd99(a)x35g2000hsb.googlegroups.com...
From Books On Line sp_send_dbmail (Transact-SQL) Example B

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks Administrator',
@recipients = 'danw(a)Adventure-Works.com',
@query = 'SELECT COUNT(*) FROM AdventureWorks.Production.WorkOrder
WHERE DueDate > ''2004-04-30''
AND DATEDIFF(dd, ''2004-04-30'', DueDate) < 2' ,
@subject = 'Work Order Count',
@attach_query_result_as_file = 1 ;


Denis The SQL Menace
http://www.lessthandot.com/
http://sqlservercode.blogspot.com
http://sqlblog.com/blogs/denis_gobo/default.aspx



On Jul 15, 4:52 pm, "Dan" <dantheri...(a)hotmail.com> wrote:
> Is there an easy way to email the results of a query using sp_send_dbmail?
> I
> can always use a cursor and build a string for the email body, but I was
> hoping there would be a better way.