How to handle errors in Stored Procedures.

Answer Posted / senthil

Error handling means if our stored procedure generates any error while running,we can handle that error.So that it will not show the error.

For eg.

insert into emp(sno,name,age) values(10,'Vasanth',26);

Consider field "sno" is primary key.When we are giving duplicate input to the sno it will show the error.

If you dont want to show the error ,you can capture the error and display as below.

BEGIN TRY
insert into emp(sno,name,age) values(10,'Vasanth',26);

END TRY

BEGIN CATCH
SELECT @err = @@error
IF @err <> 0
BEGIN
RETURN @err
END
END CATCH

Here in this case,it will capture the error and display.

Is This Answer Correct ?    12 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Does a sql server 2005 select statement require a from?

568


What is a functions and types in sql server?

589


code to create procedure for taking databse backup in sql server or i have the query for it but what it's query returns means i want to show on my jsp that the databse backup has been taken on the basis of that return value.does it returns 0 or 1.wat is the code for that

1750


What is the difference between executequery () and executeupdate ()?

507


What is sub query and its properties?

535






Can a rule be bound to any column of any data type?

558


How many servers can we create in a single subscription?

146


When we should use @@error?

534


What is the difference between clustered index and primary key?

498


Can you name a few encryption mechanisms in sql server?

534


What is the use of stored procedure?

508


How to execute a sql statement using odbc_exec()?

564


What is the difference between dbcc indexdefrag and dbcc reindex?

513


Who developed sql server?

522


Explain stored procedure?

590