![]() In this particular case, there is a special function that can be used in the CATCH statement that allows checking whether a transaction is in a committable state or not, which then allows us to make a decision to rollback or to commit it. The problem is if an error occurs after we begin but before we commit or rollback. Why? Because if there’s a BEGIN TRANSACTION, it always must end with a COMMIT or ROLLBACK transaction. Everything can be done with a simple TRY and CATCH statement and the only part when it can be tricky is when we’re dealing with transactions. That’s all that is needed when it comes to SQL Server error handling. ERROR_MESSAGE – Returns the most essential information and that is the message text of the error.ERROR_PROCEDURE – Returns the name of the stored procedure or function.ERROR_LINE – Returns the line number at which an error happened on.ERROR_SEVERITY – Returns the information about anything from informational errors to errors user of DBA can fix, etc.ERROR_STATE – Returns the information about the source.ERROR_NUMBER – Returns the internal number of the error.We even have access to some special data only available inside the CATCH statement: Now, inside the CATCH statement, we can try to fix the error, report the error or even log the error, so we know when it happened, who did it by logging the username, all the useful stuff. So, if an error would have happened inside this TRY statement, the control would have immediately get transferred to the CATCH statement and then it would have started executing code line by line. We have two blocks of code:Īnything between the BEGIN TRY and END TRY is the code that we want to monitor for an error. On top of that, there is a RAISERROR function that can be used to generate our own custom error messages which is a great way to translate confusing error messages into something a little bit more meaningful that people would understand. This is a very simple yet structured way of doing it and once you get the hang of it, it can be quite helpful in many cases. Furthermore, the method will be explained in a SQL Server case using a group of T-SQL statements/blocks, which is basically SQL Server way of handling errors. In this article, we’ll take a closer look at the TRY… CATCH statement: the syntax, how it looks, how it works and what can be done when an error occurs. Luckily, we have a chance to translate those messages into something more meaningful to pass on to the users, developers, etc. It can even be translating the error in SQL language because we all know how technical SQL Server error messages could get making no sense and hard to understand. SQL Server error handling can be as simple as just logging that something happened, or it could be us trying to fix an error. For example, when things go wrong, we get a chance to do something about it and possibly make it right again. Error handling in SQL Server gives us control over the Transact-SQL code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |