Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Exception Handling in SQL Server Database, SQL Tutorial

We learn SQL Server Exception Handling with TRY, CATCH , THROW, RAISERROR
Like any other programming language there is a technique of error handling in SQL Server query during Database Development

SQL Server has two types of exceptions:
  • System Defined
  • User Defined

Here are few Error handling property statements in SQL
ERROR_NUMBER()
ERROR_STATE()
ERROR_SEVERITY()
ERROR_LINE() tell you where the error is, very helpful
ERROR_PROCEDURE()
ERROR_MESSAGE() this will get the error details

Handling the Exception using TRY CATCH in SQL Database

error handling in sql database

Note: In SQL Server RAISERROR and THROW both statements are used to raise an error. RAISERROR was introduced from SQL Server 7.0, where as the journey of THROW statement has just began with SQL Server 2012

RAISERROR eventually will be replaced by THROW

Now we can use RAISERROR inside catch block like
RAISERROR(ERROR_MESSAGE(),ERROR_SEVERITY(),ERROR_STATE())

How to find “Error log file location” on your SQL Server

error log file in sql database
SELECT SERVERPROPERTY('ErrorLogFileName') AS 'Error log file location'

We also can get error log information by executing SP_READERRORLOG ,but to execute this sp user need to have securityadmin role

 
SQL Error Handling

Group Training