C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Try Catch block in SQLServer 2005


Posted Date: 23 Apr 2008    Resource Type: Articles    Category: Databases

Posted By: Nitin Srivastava       Member Level: Gold
Rating:     Points: 10



With SQL Server 2000, we do have Exception Handling using @@Error variable buy this is very cumbersome when you have multiple statements.
Now SQL Server 2005 provides you great deal of implementing Exception Handling with ease of process like Exception handling in other programming languages.


SQL Server 2005 comes with Try and Catch block exception handling. (Whenever an exception occurred in any of TRY block statements, the process will goes to Catch block.)

TRY BLOCK : put all your processing code here
CATCH BLOCK : write the handling mechanism like auditing.


In Catch block, we use the following error functions to fetch the exception details in-depth.

ERROR_NUMBER() : Represents Error Number,
ERROR_SEVERITY() : Represents Severity of the Error,
ERROR_STATE() : Represents State number of the Error
ERROR_PROCEDURE() : Represents thename of the stored procedure or trigger caused the
CATCH block of a TRY
ERROR_LINE() : Line number where error occured,
ERROR_MESSAGE() : Message of the error which helps to take action;



Example

Following stored procedure executes multiple SQL statements and uses TRY...CATCH block to catch
errors.

BEGIN
BEGIN TRY
--- Your Statements---
End Try
BEGIN CATCH
DECLARE @ErrMsg NVARCHAR(MAX)
DECLARE @ErrSeverity NVARCHAR(MAX)
DECLARE @ErrProc NVARCHAR(MAX)
DECLARE @ErrState INT
SELECT @ErrMsg = ERROR_MESSAGE(), @ErrProc = ERROR_PROCEDURE(), @ErrSeverity = ERROR_SEVERITY(), @ErrState = ERROR_STATE()
RAISERROR (@ErrMsg,@ErrSeverity, @ErrState)
END CATCHEND



Summary

SQL Server 2005 augments and ease the development process with new Exception Handling techniques.
For more information on Exception handling,
http://msdn2.microsoft.com/en-us/library/ms179296.aspx




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Try-Catch  .  Sql server 200  .  Code  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to use join in update statement
Previous Resource: SQL Injection
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use