Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Database Mail SQL 2005 Query

Author  Topic 

dprichard
Yak Posting Veteran

94 Posts

Posted - 2006-10-02 : 08:39:08
Okay, so I figured out how to enable Database Mail in SQL 2005 and have sent some basic mail out of tutorials I have found. I am trying to send a query result from my database, but haven't had a lot of luck.

I have a request table that stores the vacation request. I want to send the request to the site admin each night.

This is my query so far:


DECLARE @emp_id INT
SET @emp_id = 2
SELECT employee_1.emp_email
FROM employee INNER JOIN
request ON employee.emp_id = request.emp_id INNER JOIN
department ON employee.emp_department = department.department_id INNER JOIN
employee AS employee_1 ON department.department_contact_1 = employee_1.emp_id
WHERE employee.emp_id = @emp_id
GROUP BY employee_1.emp_email


This brings back the email address I need to mail to.

I need to send out an email telling this user they have a new request awaiting their approval.

dprichard
Yak Posting Veteran

94 Posts

Posted - 2006-10-02 : 09:15:36
Tried this with a simple query, but get an error:

Query:

EXEC msdb.dbo.sp_send_dbmail
@recipients = 'me@myemail.com',
@query = 'SELECT * FROM employee' ,
@subject = 'Work Order Count',
@attach_query_result_as_file = 1 ;


Here is the error:

[code]
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 476
Query execution failed: Msg 208, Level 16, State 1, Server SERVERNAME, Line 1
Invalid object name 'employee'.
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
[/code}

Go to Top of Page

dprichard
Yak Posting Veteran

94 Posts

Posted - 2006-10-02 : 09:20:55
Okay, figured that out. I needed to show the database as databasename.database.table instead of just the table itself.

Yes, it seems I am talking to myself, but now I am trying to figure out how to use the query results for say the from and to email address and body of the mail.
Go to Top of Page
   

- Advertisement -