troubleshooting Question

Multiple updates within one stored procedure. Only some are getting committed.

Avatar of rwheeler23
rwheeler23Flag for United States of America asked on
Microsoft SQL Server
25 Comments1 Solution101 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 25 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 25 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros