Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Sql agent job step - tsql command Expand / Collapse
Author
Message
Posted Wednesday, January 11, 2012 10:49 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 29, 2012 7:00 AM
Points: 37, Visits: 133
I need to figure out how to cause a job to fail and do an alert based on whether a query returns rows from a select statement. I just need a basic example of how to do this. is there a particular return value that will signal to the sql job to fail?
Post #1234204
Posted Wednesday, January 11, 2012 10:51 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:14 AM
Points: 2,614, Visits: 3,130
I just force it to fail. SELECT * FROM tableThatDoesNotExist

Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Post #1234207
Posted Wednesday, January 11, 2012 12:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 29, 2012 7:00 AM
Points: 37, Visits: 133
I'm not really testing to see if a table exists or not but if rows in a table exist:

and I figured it out here is my answer for my question:

IF @@ROWCOUNT = 0
BEGIN
RAISERROR('You need to sync contactcalldetail.', 16, 1)
RETURN -100
END
Post #1234310
Posted Wednesday, January 11, 2012 12:55 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:14 AM
Points: 2,614, Visits: 3,130
DeanORenO (1/11/2012)
I'm not really testing to see if a table exists or not but if rows in a table exist:

and I figured it out here is my answer for my question:

IF @@ROWCOUNT = 0
BEGIN
RAISERROR('You need to sync contactcalldetail.', 16, 1)
RETURN -100
END


I see what you are doing, but that will not cause the job to fail. It will simply complete. If you want the agent job to fail, you have to force it.


Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Post #1234314
Posted Wednesday, January 11, 2012 12:58 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 29, 2012 7:00 AM
Points: 37, Visits: 133
If you know the code for this, then, please by all means put it in the thread

Oh and BTW this code does work
Post #1234316
Posted Wednesday, January 11, 2012 1:01 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:14 AM
Points: 2,614, Visits: 3,130
DeanORenO (1/11/2012)
If you know the code for this, then, please by all means put it in the thread

Oh and BTW this code does work


Interesting... So this:

IF @@ROWCOUNT = 0
BEGIN
RAISERROR('You need to sync contactcalldetail.', 16, 1)
RETURN -100
END



When your rowcount is 0, the job actually fails?


Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Post #1234320
Posted Wednesday, January 11, 2012 1:06 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 29, 2012 7:00 AM
Points: 37, Visits: 133
Please review the specs on RAISEERROR
http://msdn.microsoft.com/en-us/library/ms178592
Post #1234323
Posted Wednesday, January 11, 2012 1:23 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:14 AM
Points: 2,614, Visits: 3,130
DeanORenO (1/11/2012)
Please review the specs on RAISEERROR
http://msdn.microsoft.com/en-us/library/ms178592


I think I was misunderstanding you. Now that you have this, what is the goal? Why would you want a job to fail just for an alert. Job should complete, but alert you of the condition. A job not completing should be an indication of a problem with the job itself, not a condition of the data.


Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Post #1234331
Posted Wednesday, January 11, 2012 1:37 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 29, 2012 7:00 AM
Points: 37, Visits: 133
The job failure triggers an email sent to an operator which is done
Post #1234342
Posted Wednesday, January 11, 2012 1:59 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:14 AM
Points: 2,614, Visits: 3,130
DeanORenO (1/11/2012)
The job failure triggers an email sent to an operator which is done


I don't see the reason for this. You want your job to complete, but send an email. You should be using something like sp_send_dbmail instead of letting the job fail. A job failure should indicate a problem with the job itself or the code that a step is running. Not a problem with the data. Of course this is an opinion, but it is counter-intuitive to have a job fail when the job is doing what it is supposed to do.

Am I making sense here? i.e. If I have a job check for errors in the log, I don't want the job to fail and alert me that there are errors. I want the job to complete and report those errors to me. If the job fails, I don't assume there is a problem with the data, I assume there is a problem with the job and I look there. In this case I would think that the job was in contention with another job or that something was configured wrong with the job itself.

What if there IS something wrong with the job itself, and now you go looking at your data or the script and can't figure out the issue. This is why I think it is a better practice to separate the 2.


Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Post #1234359
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse