MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - Completing the Introduction to Transact-SQ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

Completing the Introduction to Transact-SQL
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2008-01-08

    Table of Contents:
  • Completing the Introduction to Transact-SQL
  • Error Handling
  • Playing with Cursors
  • The World of Functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Completing the Introduction to Transact-SQL - Error Handling


    (Page 2 of 4 )


    Error Handling

    Let's revisit the RETURN instruction that was presented in the previous article. Simply put, it exits from the procedure or query without relying on conditions. This means that the statements that are next to the RETURN are ignored; the execution ends right as the RETURN instruction is met. Stored procedures can return integers.

    When you want to use RETURN within a stored procedure, pay attention, because a NULL value cannot be returned. If you attempt to do this, a warning message as well as a value from the 0 and -14 interval is generated and returned. It is also worth pointing out that with stored procedures, usually a nonzero is akin to a failure while a returned 0 equals success.

    Moreover, we have a global variable called @@error that basically always returns the error number for the last T-SQL statement executed. Unless documented otherwise, as I mentioned, a nonzero value means that the execution encountered an error. It is really important to immediately check or backup the value of @@error because it is cleared after each statement execution (to return the latest state).

    You see, that's why experienced programmers often say that you need to make compromises and assume that nothing will go wrong. Verifying whether everything is all right after each execution is very boring and monotonous. However, you get my drift.

    Moving on, we have the TRY...CATCH construct, which is similar to the exception handling that is possible with other programming languages such as C++, Java, and C#. Thanks to the TRY...CATCH blocks we can implement error handling a bit easier. Things can go very deep (yet be very simple) when you start to nest these blocks. Have no fear!

    First of all, we need to understand its methodology. All of the statements that are enclosed within the TRY block are executed. Then the program proceeds to move on if and only if there weren't any errors encountered; then it skips the entire CATCH block. However, if one of the statements caused an error then the control immediately jumps to the beginning of the CATCH block and heads on to execute the statements within.


    BEGIN TRY

    lots of statements

    dozens of statements

    END TRY

    BEGIN CATCH

    catch statements

    END CATCH


    The above example really points out that by using TRY...CATCH blocks we can write code that is much easier and readable. There are six kinds of "catch functions" which can be used to obtain error information: 

    • ERROR_LINE()

    • ERROR_MESSAGE()

    • ERROR_NUMBER()

    • ERROR_PROCEDURE()

    • ERROR_SEVERITY()

    • ERROR_STATE().


    The name of these functions are very intuitive and self-explanatory so we won't get into them. Should you call one of the aforementioned functions outside of a CATCH then don't be surprised because they always return NULL.

    Warning: Errors that have a lower severity than 10 are considered warnings, and thus aren't considered errors, therefore they do not alter the flow of control. Additionally, neither of the errors that disconnect the server will cause branching to the CATCH block. Processes that are terminated with a KILL command aren't trapped either. Always consider these when implementing error handling.

    The last instruction that we'll describe here is the RAISERROR. You as a programmer can opt for either obtaining one of the error messages from the sysmessages table or dynamically creating an error message of your own accompanied by user-specified severity and state information. We usually use these in the case of stored procedures.


    RAISERROR ({msg_id | msg_str}{, severity, state})


    Notes: msg_id is the user-defined error message number and should be larger than 50,000 if you are creating your own error message. The maximum value is 2^31-1; msg_str stands for the error message per se and its maximum length is 8,000 chars; severity is the user-defined value and it can go from 0 to 18 (any user) and 19 to 25 (sysadmins only); state can be 1-127 and it stands for the invocation state of an error.

    Warning: Severity levels 20-25 are considered FATAL. If this occurs then error message gets returned and logged, then the client connection is interrupted.

    More MS SQL Server Articles
    More By Barzan "Tony" Antal


       · This is the second part of my T-SQL series. Thanks for reading and don't forget to...
     

    MS SQL SERVER ARTICLES

    - Windows Server 2008 as a Workstation OS
    - An Overview of Windows Server 2008 R2
    - LINQ to MySQL, Oracle and PostgreSQL Provide...
    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT