|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Introduction
I will discuss the new feature ( TRY ........CATCH) for errorhandling in SQL SERVER 2005
As i have mentioned this TAC block is very similar to what be use in other .net langauges . The idea is faily simple When an error condition is detected in a Transact-SQL statement contained in a TRY block, control is passed to a CATCH block where it can be processed. Begin Try
The_Query_for_which_we_need_to_do_the_ Error_Handling
End Try
Begin Catch
If there is some error in the query within the Try block , this flow
will be passed to this Catch block .
Do for all the error , the TAC blocks works ?. Now we will talk on that
below
End catch
If there are no errors inside the TRY block, control passes to the statement
immediately after the associated END CATCH statement. If the END CATCH
statement is the last statement in a stored procedure or trigger, control is
passed to the statement that invoked the stored procedure or trigger.
No , please do take care of the following points
BEGIN TRY
Print ' I am level 1 '
BEGIN TRY
Print ' I am level 2 '
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;
Print ' I am going out level 2 '
END CATCH
Print ' I am level 1 again '
END TRY
BEGIN CATCH
-- Execute the error retrieval routine.
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
Output I am level 1
I am level 2
-----------
(0 row(s) affected)
ErrorNumber ErrorSeverity ErrorState ErrorProcedure ErrorLine ErrorMessage
8134 16 1 NULL 5 Divide by zero
error encountered.
(1 row(s) affected)
I am going out level 2
I am level 1 again
In the code above the error is thrown from the Level 2 TAC block
From the above two statement it is clear that Try & Catch block will work for Errors with a severity of 10 < TRy & Block <= Errors with a severity of 20
ERROR_NUMBER() returns the error number.
No , the TAC block will not catch the compile errors , if it is not called in the from of dynamic query or in some SP
BEGIN TRY
-- This PRINT statement will run since the error
-- occurs at the SELECT statement.
PRINT N'Starting execution'
SELECT * FROM NonExistentTable
-- This SELECT statement will generate an object name
-- resolution error since the table does not exist.
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO
OutPut :
Starting execution
Msg 208, Level 16, State 1, Line 6
Invalid object name 'NonExistentTable'.
I will try to put the above logic using a dynamic sql . On doing so the code will compile , but will through a error , which will be catched by the TAC block
BEGIN TRY
-- This PRINT statement will run since the error
-- occurs at the SELECT statement.
PRINT N'Starting execution';
DECLARE @SQL NVARCHAR(2000)
SET @SQL = 'SELECT * FROM NonExistentTable;'
-- This SELECT statement will generate an object name
-- resolution error since the table does not exist.
EXEC sp_executesql @SQL
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO
Output :
Starting execution
ErrorNumber ErrorMessage
----------- ------
208 Invalid object name 'NonExistentTable'.
(1 row(s) affected)
In this state, however, the locks acquired by the transaction are maintained, and the connection is also kept open. The transaction's work is not reversed until a ROLLBACK statement is issued. The code in a CATCH block should test for the state of a transaction by using the XACT_STATE function. XACT_STATE returns a -1 if the session has an uncommittable transaction. The CATCH block must not perform any actions that would generate writes to the log if XACT_STATE returns a -1. The following code example generates an error from a DDL statement and uses XACT_STATE to test the state of a transaction in order to take the most appropriate action. CREATE TABLE TEMP ( A INT )
BEGIN TRY
-- BEGIN Tran
ALTER TABLE TEMP
DROP COLUMN author
COMMIT TRAN
END TRY
BEGIN CATCH
-- Execute the error retrieval routine.
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;
PRINT ' *****Value of XACT_STATE ****'+CONVERT(VARCHAR,XACT_STATE())
END CATCH
GO
Output
ErrorNumber ErrorSeverity ErrorState ErrorProcedure ErrorLine ErrorMessage
----------- ------------- ----------- --------------------------------------
3930 16 1 NULL 3 The current transaction
cannot be committed and
cannot support
operations that write
to the log file. Roll
back the transaction.
(1 row(s) affected)
*****Value of XACT_STATE ****-1
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||