Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do i send the query results using SQL 2005 and Database Mail

Posted on 2008-06-19
7
Medium Priority
?
798 Views
Last Modified: 2012-05-05
I have database mail setup and I can send myself a test message in either text or html... but when I try to send a query as the message, the email fails.

This is my test code that works:


DECLARE @Message AS VARCHAR(100)
SET @Message ='Server :' + @@servername+ ' My First Database Email '
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'LOGON',
@subject = 'Testing',
@recipients = 'email address here',
@body_format = 'TEXT',
@body = @message,


This is my testcode that fails

DECLARE @Message AS VARCHAR(100)
SET @Message ='Server :' + @@servername+ ' My First Database Email '
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'LOGON',
@subject = 'Testing',
@recipients = 'email address here',
@body_format = 'TEXT',
@body = @message,
@Query = 'Select * from [Jason_DB].[dbo].[Sales_JobEntry]'

0
Comment
Question by:logoncom
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21824086
Do you have permission to run the query listed in the stored procedure?
0
 

Author Comment

by:logoncom
ID: 21824432
I wasnt trying to access a sp, will it only work with sprocs?

Yes, I have full access
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21824443
i meant the msdb.dbo.sp_send_dbmail stored procedure.

Make sure your database mail profile is public.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:logoncom
ID: 21824477
The profile is public.  I can send email... I have already tried... my problem is I do not know how to send query results as the body of the email.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21824511
You're getting an error when you do, right?  What is the error?
0
 

Author Comment

by:logoncom
ID: 21824553
I get bad syntax or wrong variable etc... problem is I dont know how to tell database mail to send the results of the qeury as the body of the email and not a message.

How would I email:  Select * from Table where field = 'xyz'
0
 

Accepted Solution

by:
logoncom earned 0 total points
ID: 21826245
In case anyone wants the solution to this... I figured it out myself


--sp_send_dbmail
--    [ [ @profile_name = ] 'profile_name' ]
--    [ , [ @recipients = ] 'recipients [ ; ...n ]' ]
--    [ , [ @copy_recipients = ] 'copy_recipient [ ; ...n ]' ]
--    [ , [ @blind_copy_recipients = ] 'blind_copy_recipient [ ; ...n ]' ]
--    [ , [ @subject = ] 'subject' ]
--    [ , [ @body = ] 'body' ]
--    [ , [ @body_format = ] 'body_format' ]
--    [ , [ @importance = ] 'importance' ]
--    [ , [ @sensitivity = ] 'sensitivity' ]
--    [ , [ @file_attachments = ] 'attachment [ ; ...n ]' ]
--    [ , [ @query = ] 'query' ]
--    [ , [ @execute_query_database = ] 'execute_query_database' ]
--    [ , [ @attach_query_result_as_file = ] attach_query_result_as_file ]
--    [ , [ @query_attachment_filename = ] query_attachment_filename ]
--    [ , [ @query_result_header = ] query_result_header ]
--    [ , [ @query_result_width = ] query_result_width ]
--    [ , [ @query_result_separator = ] 'query_result_separator' ]
--    [ , [ @exclude_query_output = ] exclude_query_output ]
--    [ , [ @append_query_error = ] append_query_error ]
--    [ , [ @query_no_truncate = ] query_no_truncate ]
--    [ , [ @query_result_no_padding = ] query_result_no_padding ]
--    [ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ]


--QUERY EMAIL MESSAGE

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Profile',
    @recipients = 'recipientsemail@email.com',
    @query = 'SELECT * FROM TableName' ,
    @subject = 'Work Order Count',
    @attach_query_result_as_file = 0 ;


--HTML EMAIL MESSAGE

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 td = wo.WorkOrderID,       '',
                    td = p.ProductID, '',
                    td = p.Name, '',
                    td = wo.OrderQty, '',
                    td = wo.DueDate, '',
                    td = (p.ListPrice - p.StandardCost) * wo.OrderQty
              FROM AdventureWorks.Production.WorkOrder as wo
              JOIN AdventureWorks.Production.Product AS p
              ON wo.ProductID = p.ProductID
              WHERE DueDate > '2004-04-30'
                AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
              ORDER BY DueDate ASC,
                       (p.ListPrice - p.StandardCost) * wo.OrderQty DESC
              FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table>' ;

EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
    @subject = 'Work Order List',
    @body = @tableHTML,
    @body_format = 'HTML' ;


--send email

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'AdventureWorks Administrator',
    @recipients = 'danw@Adventure-Works.com',
    @body = 'The stored procedure finished successfully.',
    @subject = 'Automated Success Message' ;
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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

916 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