I have created this stored procedure which is nothing more than a crude attempt at standardizing addresses. It contains about 30 updates that corrects the string value of the addresses. The problem I have is that it just does some of them. It is always skips the same ones. When I had SET NOCOUNT OFF it would display the correct record count per update. It always comes back and says successfully completed. I wrapped each update in a BEGIN TRAN .. END TRAN. If I run the update individually it correctly updates all of them. What must I do to get it commit each of the updates?
CREATE PROCEDURE [dbo].[apd_UpdateAddresses]
AS
BEGIN
SET NOCOUNT ON
DECLARE @TranCount int
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SET @TranCount = @@TRANCOUNT
IF @TranCount = 0
BEGIN TRAN UpdateAddresses
ELSE
SAVE TRAN UpdateAddresses
BEGIN TRY
BEGIN TRAN
-- 1ST FLOOR
UPDATE MyTable
SET [Something] = CASE WHEN PATINDEX('%1ST FLOOR%',[Something]) > 1 THEN SUBSTRING([Something],1,PATINDEX('%1ST FLOOR%',[Something])-1) ELSE [Something] END
WHERE [Something] LIKE '%1ST FLOOR%'
COMMIT TRAN
-- IST FLOOR
BEGIN TRAN
UPDATE MyTable]
SET [Something] = CASE WHEN PATINDEX('%1ST FLOOR%',[Something]) > 1 THEN SUBSTRING([Something],1,PATINDEX('%1ST FLOOR%',[Something])-1) ELSE [Something] END
WHERE [Something] LIKE '%1ST FLOOR%'
WHERE [Something] LIKE '%IST FLOOR%'
COMMIT TRAN
.
. 30 more such updates
.
END TRY
BEGIN CATCH
ROLLBACK TRAN UpdateAddresses
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
-- Complete the transaction
COMMIT TRAN UpdateAddresses
RETURN 0 -- No errors
SET ANSI_NULLS ON
END
GO
> WHERE [Something] LIKE '%IST FLOOR%'
For starters..
> BEGIN TRAN
This should be named something so it is not confused with the TRAN named UpdateAddresses. Or is that it but the developer forgot the name? Otherwise there should be a ROLLBACK TRAN somewhere in the CATCH block error handling that I don't see.