Why Use Error Handling?

  • I have always believed best practice when writing stored procedures or any SQL code is to include error handling. I was challenged today though as to why include error handling in a certain ETL project being written at the moment, I think its quite obvious that if there is some sort of failure then the load should roll back instead of leaving it in a non consistent state.

    I was then asked as to what scenarios might a failure occur, there is the obvious of network blips, servers failing, databases failing, deadlocks occurring, table being accidentally dropped, can you think of any specific reasons as to why you have used error handling in any projects and when it has provided useful.

    thanks in advance.

  • dhechle (4/30/2014)


    I have always believed best practice when writing stored procedures or any SQL code is to include error handling. I was challenged today though as to why include error handling in a certain ETL project being written at the moment, I think its quite obvious that if there is some sort of failure then the load should roll back instead of leaving it in a non consistent state.

    I was then asked as to what scenarios might a failure occur, there is the obvious of network blips, servers failing, databases failing, deadlocks occurring, table being accidentally dropped, can you think of any specific reasons as to why you have used error handling in any projects and when it has provided useful.

    thanks in advance.

    Well gosh...you are talking an ETL process. That means it is an automatic process right? Wouldn't business like to be informed if something didn't work? Let's say your scenarios of a table being dropped happened. If you didn't have error handling how long would the process keep crashing before somebody noticed?

    What a strange challenge. This is like asking "why do you have car insurance?" or "why do you wear a seatbelt?". 99.99999% of the time it isn't necessary, but that one time when the "OH S..." happens it is better to be prepared by being able to handle the issue instead of just crashing.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • One argument against error handling in T-SQL is that there is already error handling in the calling application. To which I'll respond that there are unique error events within SQL Server best handled within SQL Server such as a deadlock (assuming we're talking about TRY/CATCH here). But not all processes within T-SQL have to have error handling. That said, a multi-step ETL process done within T-SQL, yeah, that's a place where I'd probably put error handling and validations that everything is working.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply