Error functions in sql server


In this post, we will discuss various error functions in sql server 2008. Also, you can check my previous posts on:
Sql server provides a set of functions to get information about raised errors which are known as Error Functions.
Within the scope of a TRY CATCH block, you can use the following error  functions:
1. ERROR_NUMBER():
Every error in sql server is associated with a unique number which is uniquely identified. This function returns the error number of the currently raised error in your program.
2. ERROR_STATE():
Every error in sql server is associated with a state which can be used to identify exact location in the program where that error occurs. This function will return the state of the raised error.
3. ERROR_PROCEDURE():
This function returns the name of the stored procedure or trigger in which error occurs. This function returns NULL if the error did not occur inside a stored procedure or trigger.
4. ERROR_MESSAGE():
This function is used to get the message related to the raised error in the program. This function returns the text of the message that would be returned to the application. The text includes the values supplied for any substitutable parameters such as names, length or time.
5. ERROR_LINE():
This function is used to get the line number with in the stored sub program where the error occurs.
6. ERROR_SEVERITY():
Every error in sql server has severity how much severe the error is. This function will return the severity of the currently raised error in the program.
Immediately after executing any TSQL statement, you can test for an error and retrieve that error number using the @@ERROR function.
Getting a list of error messages:
By using a system table   called sys.messages you can get the complete list of error messages available in the sql server to which you are connected.
Ex:  Select * from sys.messages
By using the stored procedure named sp_addmessage you can add your own messages to the built in error messages of sql server.
Ex.  Sp_addmessage msgid, severity, msgtext
Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →