Skip to main content

TRY/CATCH and error handling in SQL server 2005/2008

SQL Server 2005 provide a strong set of tool to handle error or exception, in privious version it was not easy to handle exceptions.
We use TRY/CATCH in SQL Server same as populer languages, We write saperate logic and exception handling code.

Syntax:
BEGIN TRY
-- Your Code or query
END TRY
BEGIN CATCH
-- Handling code
END CATCH

We write our logic code within TRy bolck and exception code in CATCh block, if nay error occur in logic code it jumps to CATCH 

block, resume the execution and transaction is rollback.

Functions which is use in CATCH

1- ERROR_NUMBER() Returns error no othe error message.
2- ERROR_SEVERITY() returns the error severity regardless of how many times it is run.
3- ERROR_STATE() Returns the state number of error messages that cause the catch block.
4- ERROR_PROCEDURE() Return the Procedure Name.
5- ERROR_LINE() Return error line number.
6- ERROR_MESSAGE() return error description.

Note : If we are calling all above method out side from CATCH block it will return NULL..

Example
CREATE PROCEDURE dbo.usp_TRY_CATCH_TEST
AS
BEGIN
BEGIN TRY
   SELECT 1/0
END TRY
BEGIN CATCH
   SELECT ERROR_NUMBER() AS error_no
   ,ERROR_SEVERITY() AS error_severity
   ,ERROR_STATE() AS error_state
   ,ERROR_PROCEDURE() AS error_procedure
   ,ERROR_LINE() AS error_lineNo
   ,ERROR_MESSAGE() AS error_message
END CATCH
END

Comments

Post a Comment

Popular posts from this blog

Inner join in sql server

INNER JOIN Inner join responsible to returns rows which have a match in both tables. See following example:- CREATE TABLE #ORDER ( orderId INT IDENTITY ( 1 , 1 ), fName VARCHAR ( 400 ), lName VARCHAR ( 400 ), ShipAddress VARCHAR ( 1000 ), dtCreate DATETIME ) CREATE TABLE #ORDER_DETAILS ( orderDetailsId INT IDENTITY ( 1 , 1 ), productName VARCHAR ( 400 ), pPrice DECIMAL ( 18 , 2 ), iQTY INT , orderId INT , dtCreate DATETIME ) INSERT INTO #ORDER ( fName , lName , ShipAddress , dtCreate ) SELECT 'Dilip' , 'SINGH' , 'New Delhi' , GETDATE () UNION ALL SELECT 'Nirmit' , 'katiyar' , 'Mumbai' , GETDATE () UNION ALL SELECT 'Raj' , 'Dongra' , 'Jharkhand' , GETDATE () UNION ALL SELECT 'Salim' , 'safi' , 'Muradnagar UP' , GETDATE () INSERT INTO #ORDER_DETAILS ( productName , pPrice , iQTY , orderId , dtCreate ) SELE

Constraints in sql server

Constraints   - Constraints are the rules enforced on data columns on table. This ensures the accuracy and reliability of the data in the database. Constraints define some conditions that restricts the column to remain true while inserting or updating or deleting data in column. Constraints can be defined in two ways   1) The constraints can be specified immediately after the column definition. This is called column-level definition.   2) The constraints can be specified after all the columns are defined. This is called table-level definition.   Types of Constraints in SQL-Server NOT NULL - This constraint is responsible for a column to confirm that a column cannot have a null value. DEFAULT – This constraint provides a default value when specified non for this column. UNIQUE - This constraint insure that each row for the column has different value. PRIMARY KEY - Primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constra

NOT NULL constraint in sql server

NOT NULL- This constraint is responsible for a column to confirm that a column cannot have a null value. Example- Following we create new table PRODUCT and add six columns where ID, SKU, TITLE, PRICE specify not to accept NULLs. CREATE TABLE PRODUCT ( ID INT NOT NULL, SKU VARCHAR ( 20 ) NOT NULL, TITLE VARCHAR ( 200 ) NOT NULL, PRICE MONEY NOT NULL, DISCRIPTION VARCHAR ( 2000 )   NULL, DTCREATE DATETIME NULL ) If PRODUCT table already created, then to add a NOT NULL constraints to SKU column we use following query.   ALTER TABLE PRODUCT   ALTER COLUMN SKU  VARCHAR ( 20 ) NOT NULL