Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Emailing directly from job

Status
Not open for further replies.

Ofina

Technical User
Feb 25, 2004
156
US
I'm setting up an SQL job that I want to email me.

Let's say step one is to run
select * from orders

Can step 2 be to email me the results of that select statement? Failing that, can I put the code to do this into the sql of step 1?

I'd like to avoid a stored procedure if I can.

SQL 2008
 
For SQL 2008 use sp_send_dbmail.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Sorry, I agree sp_send_dbmail is better for sql2008.

Viewer, scheduler and report manager for Crystal reports and SSRS.
Send your report everywhere.
 
I can put that right in the job? Same step or new step?

I know about sp_send_dbmail, but I've only ever used it in a stored procedure.
 
Yes, you can put it in a job step. Either way that works for you - the same step or a new one.

Very basically, any script that you can run in a query window you can also put into a job step.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Okay, this is working now. But, now I want to not email anything out if the result of the select statement is empty.
 
Try using an IF statement,something like...

IF (SELECT COUNT() .....) > 0

<send email>

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
One alternative to using count() would be

if exists(select null .....)

I'm sure there are others.
 
That's actually the one I wound up using today. The initial suggestion errored out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top