Home > SQL Server Tips > Stored Procedures > Using 'error handling' in stored procedures
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

STORED PROCEDURES

Using 'error handling' in stored procedures


Barrie Sosinsky
03.18.2004
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Error handling is a part of any well-written code and should be part of any stable database. You can't always account for why things go wrong, but you can provide an exit strategy in your code that returns your database to a consistent state, bails out on the routine or provides the necessary clues so that you can track down what went wrong and why.

Stored procedures are a good place to use your error handling code because these routines are the ones that are the most stable and run the most often in a database. Stored procedures are often used to enforce the business rules of your DBMS, so many stored procedures are written in a modular way building upon other routines in your system.

Assuming you have developed a set of error handling routines, calling those routines from a standard procedure should depend upon evaluating a condition that exists after the stored procedure has run a significant piece of its code. Typically programmers set up their code so that a variable is populated with a return code, most often an integer value. For example, a "0" might indicate successful completion of the procedure, while "55" or "505" would indicate two different problems that occurred. Evaluation of the test condition necessary to exit the stored procedure would test this variable and exit for 0 or run the error routine for any other number.

Another aspect of error handling in stored procedures is to make sure that you are implementing your transactional recovery routines properly. Make sure that ROLLBACK and @@TRANCOUNT actually work.

Also be certain to pay particular attention to things like T-SQL BULK INSERT operations that require the use of the DTC. You can tell whether a BULK INSERT worked by examining the @@ERROR global variable, provided you check that variable directly after the statement of concern. Your code needs to reset the @@ERROR to 0 once the code runs. You can use the @@ERROR variable to initiate error handling for errors that typically don't stop a stored procedure (or a batch operation for that matter) from running. Typical examples where this might come in handy are when an aggregate function doesn't run properly or a constraint has been violated. Use the @ErrorCollector variable if you need to hold the error value for a return code. The @ErrorCollector value will examine transactions for their data integrity and when a stored procedure doesn't run properly it populates that variable with an error code.


Barrie Sosinsky is president of consulting company Sosinsky and Associates (Medfield MA). He has written extensively on a variety of computer topics. His company specializes in custom software (database and Web related), training and technical documentation.


Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL Server stored procedures
SQL and SQL Server Tutorial and Reference Guide
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure
SQL Server source code analysis and management adds database security
Configure SQL Server Service Broker for sending stored procedure data
Find size of SQL Server tables and other objects with stored procedure
Track changes to SQL Server 2000 and 2005 with one simple utility
Troubleshoot SQL Server 2005 temporary table performance problems
Use SQL Profiler to find long running stored procedures and commands
Stored procedure to monitor long-running jobs in SQL Server 2000

Stored Procedures
Check SQL Server database and log file size with this stored procedure
Configure SQL Server Service Broker for sending stored procedure data
Find size of SQL Server tables and other objects with stored procedure
Stored procedure to monitor long-running jobs in SQL Server 2000
Make changes to SQL Server stored procedures with batch editing
Stored procedure to find fragmented indexes in SQL Server
Use table-valued parameters for SPs in SQL Server 2008
Examples of SQL Server stored procedures and parameters
Top 10 SQL Server development questions
FAQ: SQL Server stored procedure how-tos

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
library  (SearchSQLServer.com)
trigger  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2005 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts