We ran into an issue in our production environment where only the first 2 updates ran and the last 2 didn't. What can we implement when the following script is run from BIDS (sql server) to ensure each of the 4 sql update statements is run? Do we need to wrap the whole thing in Try...….Catch?
Below is basically now my final working code used in production.
SET XACT_ABORT ON;
GO
BEGIN TRANSACTION
-- Batch 0
BEGIN TRY
--There is a step prior to this step to delete table
Insert Into Table X (Field1, Field2, Field3)
Select
Field1
,Field2
,Field3
FROM Table Loans
END TRY
BEGIN CATCH
PRINT 'Error Number: ' + str(error_number()) ;
PRINT 'Line Number: ' + str(error_line());
PRINT error_message();
ROLLBACK TRANSACTION;
END CATCH;
GO
-- Batch 1
BEGIN TRY
-- Rollback transaction if error occurred
IF (XACT_STATE()) = -1
BEGIN
RAISERROR('The transaction is in an uncommittable state. Rolling back transaction.', 18, 3);
END;
-- Do not continue if the transaction was rolled back
IF (XACT_STATE()) = 0
BEGIN
RAISERROR('The transaction was rolled back.', 18, 1);
END;
--Update A Fields
Update X Table
Set X Field4 = A Field
From X Table
Left OUTER JOIN A Table
ON X Key = A Key
END TRY
BEGIN CATCH
PRINT 'Error Number: ' + str(error_number()) ;
PRINT 'Line Number: ' + str(error_line());
PRINT error_message();
IF (XACT_STATE()) <> 0
BEGIN
PRINT 'Rolling Back Transaction...';
ROLLBACK TRANSACTION;
END;
END CATCH;
GO
-- Batch 2
BEGIN TRY
-- Rollback transaction if error occurred
IF (XACT_STATE()) = -1
BEGIN
RAISERROR('The transaction is in an uncommittable state. Rolling back transaction.', 18, 3);
END;
-- Do not continue if the transaction was rolled back
IF (XACT_STATE()) = 0
BEGIN
RAISERROR('The transaction was rolled back.', 18, 1);
END;
--Update B Fields
Update X Table
Set X Field5 = B Field
From X Table
Left OUTER JOIN B Table
ON X Key = B Key
END TRY
BEGIN CATCH
PRINT 'Error Number: ' + str(error_number()) ;
PRINT 'Line Number: ' + str(error_line());
PRINT error_message();
IF (XACT_STATE()) <> 0
BEGIN
PRINT 'Rolling Back Transaction...';
ROLLBACK TRANSACTION;
END;
END CATCH;
GO
-- Batch 3
BEGIN TRY
-- Rollback transaction if error occurred
IF (XACT_STATE()) = -1
BEGIN
RAISERROR('The transaction is in an uncommittable state. Rolling back transaction.', 18, 3);
END;
-- Do not continue if the transaction was rolled back
IF (XACT_STATE()) = 0
BEGIN
RAISERROR('The transaction was rolled back.', 18, 1);
END;
--Update C Fields
Update X Table
Set X Field5 = C Field
From X Table
Left OUTER JOIN C Table
ON X Key = C Key
END TRY
BEGIN CATCH
PRINT 'Error Number: ' + str(error_number()) ;
PRINT 'Line Number: ' + str(error_line());
PRINT error_message();
IF (XACT_STATE()) <> 0
BEGIN
PRINT 'Rolling Back Transaction...';
ROLLBACK TRANSACTION;
END;
END CATCH;
GO
-- Batch 4
BEGIN TRY
-- Rollback transaction if error occurred
IF (XACT_STATE()) = -1
BEGIN
RAISERROR('The transaction is in an uncommittable state. Rolling back transaction.', 18, 3);
END;
-- Do not continue if the transaction was rolled back
IF (XACT_STATE()) = 0
BEGIN
RAISERROR('The transaction was rolled back.', 18, 1);
END;
--Update D Fields
Update X Table
Set X Field7 = D Field
From X Table
Left OUTER JOIN D Table
ON X Key = D Key
END TRY
BEGIN CATCH
PRINT 'Error Number: ' + str(error_number()) ;
PRINT 'Line Number: ' + str(error_line());
PRINT error_message();
IF (XACT_STATE()) <> 0
BEGIN
PRINT 'Rolling Back Transaction...';
ROLLBACK TRANSACTION;
END;
END CATCH;
GO
-- Commit transaction
IF XACT_STATE() = 1
BEGIN
COMMIT TRANSACTION;
PRINT 'Transaction committed.';
END;