Solved

How to handle the error when a step in the SQL job fails

Posted on 2011-09-21
7
209 Views
Last Modified: 2012-05-12
I created a job with about 4 steps that process data and uses ftp to send data to our customer.

If the job fails during any step , I want to log that into  a table in the database with the error which caused the step to fail.

0
Comment
Question by:Bkuniyil
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

by:dbaSQL
ID: 36577234
I would just create an Error table to house the error functions, and insert them from the TRY/CATCH, similar to what's in this link:
http://www.sqlservernation.com/home/using-trycatch-in-sql-server.html


even better, the uspLogError procedure writes error details into the ErrorLog table.  you can use this within each of your jobs steps as the action upon failure.  then you can send an email with the error_line or error_number, with the 'JobName Failure' in the subject ine.

http://msdn.microsoft.com/en-us/library/ms179296.aspx
0
 

Author Comment

by:Bkuniyil
ID: 36580520

  Actually the job step does not execute a stored procedure. It executes an SSIS package.  When the package returns with an exit code other than 0 it is a failure. Hence the job fails . I need to trap this error and log into the database.

Experts, could you please help me in achieving this.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 36580551
Assuming it's a SQL job you can do that directly in the job step under the "Advanced" like in the attached picture - either log to a table and/or include step output in the history and you can also send emil notifications on failure. If you need to log from the batch (cmd file) that does the FTP then you may need additional code and run SQL command shell to populate the table.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 40

Expert Comment

by:lcohan
ID: 36580559
Ok, in that case the SSIS step that is executed need to include a BEGIN/TRY/CATCH set of SQL commands as "dbasql" suggested. Also you can configure the SSIS to do the Logging including in the Event long on that server.

Integration Services Log Providers

http://msdn.microsoft.com/en-us/library/ms140246(v=sql.90).aspx
0
 

Author Comment

by:Bkuniyil
ID: 36580753

   The requirement is to log the error in the table created in the database.  I guess the "Log to table"  option under the Notifactions  does not help me here.  Also the SSIS package is used to execute a batch file , so i would not be able to include a  TRY/CATCH there.
0
 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 36581029
Of course you can easily do that - from the batch file you execute commands against SQL to populate that table or anything else via SQLCMD utility:

sqlcmd Utility Tutorial
http://msdn.microsoft.com/en-us/library/ms170207(v=SQL.90).aspx
0
 

Author Closing Comment

by:Bkuniyil
ID: 37070049
The solution partially resolved my problem.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql Audit table 3 83
Stored procedure 4 36
How to disable/enable multiple sql jobs in efficient way 11 129
CROSS APPLY 4 61
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

762 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