Hi @Alice ,
As mentioned by Erland, you could use TRY-CATCH to validate the transaction is successful or not.
If the transaction is failed, it will automatically rollback and report the error details. This is very common in production environment.
Below is one classic example:
BEGIN TRY
BEGIN TRANSACTION
-- sql update\delete statement goes here
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF (@@TRANCOUNT > 0)
BEGIN
ROLLBACK TRANSACTION
PRINT 'Error ...'
END
SELECT
ERROR_NUMBER() AS Error#,
ERROR_MESSAGE() AS ErrorMsg
END CATCH
Inside the CATCH block, you can use the following functions to get the detailed information on the error that occurred:
- ERROR_LINE() returns the line number on which the exception occurred.
- ERROR_MESSAGE() returns the complete text of the generated error message.
- ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.
- ERROR_NUMBER() returns the number of the error that occurred.
- ERROR_SEVERITY() returns the severity level of the error that occurred.
- ERROR_STATE() returns the state number of the error that occurred. Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.