4

When I have multiple errors in my query, and I use try catch and error_message() function in my catch block, I only get the last error message.

For example, if my errors are:

Msg 2714, Level 16, State 5, Line 14
There is already an object named 'IX_EntityTypeConfigs' in the database.
Msg 1750, Level 16, State 1, Line 14
Could not create constraint or index. See previous errors.

in the catch block I just get:

Could not create constraint or index. See previous errors.

How can I get the whole message?

I have searched and saw that this is a limitation of SQL Server, but articles belonged to 2014 and older than that.

I wonder if things has changed since.

0

1 Answer 1

6

I believe it's still correct. The recommended way is to rethrow the errors to the application via the THROW command and let the app handle it.

The best resource is still this article by Erland Sommarskog - Error and Transaction Handling in SQL Server

You can also capture the errors with Extended Events (XE) error_reported (on SQL Server side). That would show you all the errors that were fired.

I blog about it here: Investigating Errors With Extended Events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.