The following query to about 2.5 hours to run and the commit block was hit because the message in that block in it was shown. It looked like it ran successfully, but I got "There are uncommitted transactions" message. The implicit transactions appear to be off, so I don't understand why I got this message. The code is wrapped in a transaction and the implicit transactions are turned off. I thought about turning off implicit transactions with code at the top of the script, but wonder if it will do anything since they appear to be turned off.
Any thoughts?
Results from the query:
DECLARE @PurgeDate AS VARCHAR(20) DECLARE @ArchiveInserts AS VARCHAR(30) DECLARE @GLTRdeletes AS VARCHAR(30) DECLARE @ArchiveInsertCount AS INT = 0 DECLARE @GLTRdeleteCount AS INT = 0 ----------------------------------- /**** SET BEFORE RUNNING ****/ -- the value here is for testing only ----------------------------------- SET @PurgeDate = '10/22/2011' BEGIN TRAN GLTRpurge BEGIN TRY ---------------------------------------------------------------------------- -- Archive the records to be purged ---------------------------------------------------------------------------- -- original commented out to use test values SELECT COUNT(*) AS 'Archive Inserts' FROM GLTR WHERE PostDate <= @PurgeDate AND gltrInterfaceDate IS NOT NULL INSERT INTO GLTR_ARCHIVE SELECT * FROM GLTR WHERE PostDate <= @PurgeDate AND gltrInterfaceDate IS NOT NULL ORDER BY PostDate, ID SELECT @ArchiveInsertCount = @@ROWCOUNT PRINT cast(@ArchiveInsertCount AS VARCHAR(30)) + ' Rows inserted into GLTR_Archive' --------------------------------------------------------------------------- -- Purge the original records to be archived ---------------------------------------------------------------------------- -- commented out for testing SELECT COUNT(*) AS 'GLTR Deletes' FROM GLTR WHERE PostDate <= @PurgeDate AND gltrInterfaceDate IS NOT NULL DELETE FROM GLTR WHERE ID IN (SELECT ID FROM GLTR WHERE PostDate <= @PurgeDate AND gltrInterfaceDate IS NOT NULL) SELECT @GLTRdeleteCount = @@ROWCOUNT PRINT cast(@GLTRdeleteCount AS VARCHAR(30)) + ' Rows deleted from GLTR' IF (@ArchiveInsertCount = @GLTRdeleteCount) BEGIN COMMIT TRAN GLTRpurge SELECT 'The commit was made' END END TRY BEGIN CATCH ROLLBACK TRAN GLTRpurge SELECT 'The transaction was rolled back - record counts did not match' END CATCH