This is my last transaction related work in sql, maybe the sample code below may help you. The code was developed for the MS SQL server, and you may need to modify it a bit for the MySQL server.
The main idea is to place the main request (which can be more than one) inside the sentences "try" and "transaction", and then, if the request is successful, then the request will be transferred to the database, otherwise in case of an error, the error will be raised in the catch section before the transaction is completely rejected.
BEGIN TRY BEGIN TRANSACTION --Insert Your Queries Here-- COMMIT END TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); IF @@TRANCOUNT > 0 ROLLBACK RAISERROR (@ErrorMessage, -- Message text. @ErrorSeverity, -- Severity. @ErrorState -- State. ); END CATCH
source share