Exception Handling in SQL Server
How can we use exception handling within stored procedure ? Give me a example.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Jun 5, 2012, 8:55 AM
You can use try catch in sql for exception handling.
Example:
BEGIN TRY
SELECT * FROM Table1 WHERE id = 24;
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH;
TRY…CATCH uses the following error functions to capture error information:
- ERROR_NUMBER() : returns the error number.
- ERROR_MESSAGE(): returns the complete text of the error message. The text includes the values supplied for any substitutable parameters such as lengths, object names, or times.
- ERROR_SEVERITY() : returns the error severity.
- ERROR_STATE(): returns the error state number.
- ERROR_LINE(): returns the line number inside the routine that caused the error.
- ERROR_PROCEDURE(): returns the name of the stored procedure or trigger where the error occurred.
please referhttp://www.codeproject.com/Articles/12080/Using-TRY-CATCH-in-Transact-SQL
hope this will help u.
Anil KumarPosted Jun 5, 2012, 8:49 AM