Ram Shankar Yadav's Blog!

My life's my passion!

  Home  |   Contact  |   Syndication    |   Login
  157 Posts | 0 Stories | 304 Comments | 95 Trackbacks

News


Microsoft Certified Technology Specialist ( MCTS) - .NET 2.0 Windows Development
Microsoft Certified Professional

My Photo Ablum


Cheap Website Hosting - Free Domain

Archives

Post Categories

Image Galleries

Blogs that I like most !

Sites that I like most !

Hey guys now the days of tedious error handing are gone atleast with new SQL Server 2005. My colleague Sugandh sent this nice link to me which show how we can implement Try..... Catch in our Transact SQL code, or better to say in our database queries and stored procedures.

TRY...CATCH (Transact-SQL) 

"Implements error handling for Transact-SQL that is similar to the exception handling in the Microsoft Visual C# and Microsoft Visual C++ languages. A group of Transact-SQL statements can be enclosed in a TRY block. If an error occurs in the TRY block, control is passed to another group of statements that is enclosed in a CATCH block."

 

Syntax

BEGIN TRY
     { sql_statement | statement_block }
END TRY
BEGIN CATCH
     { sql_statement | statement_block }
END CATCH
[ ; ]
 

Arguments

sql_statement

Is any Transact-SQL statement.

statement_block

Any group of Transact-SQL statements in a batch or enclosed in a BEGIN…END block.

Examples

A. Using TRY…CATCH

The following example shows a SELECT statement that will generate a divide-by-zero error. The error causes execution to jump to the associated CATCH block.

USE AdventureWorks;
GO

BEGIN TRY
    -- Generate a divide-by-zero error.
    SELECT 1/0;
END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber,
        ERROR_SEVERITY() AS ErrorSeverity,
        ERROR_STATE() AS ErrorState,
        ERROR_PROCEDURE() AS ErrorProcedure,
        ERROR_LINE() AS ErrorLine,
        ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO

Links:


TRY...CATCH (Transact-SQL) 

Using TRY...CATCH in Transact-SQL 


No more @@ERROR required.........

Happy Coding

posted on Thursday, October 12, 2006 8:12 AM

Feedback

# re: SQL Server 2005 (T-SQL) Exception handling using Try ... Catch 9/9/2008 12:47 PM MerlinTintin
How to place GO instruction into a TRY / CATCH block ?

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 3 and 6 and type the answer here: