Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:36:16+00:00 2026-06-02T16:36:16+00:00

What is the proper way to test for insert/update failures and rollback this transaction

  • 0

What is the proper way to test for insert/update failures and rollback this transaction if there are any? I don’t think what I have will work since my inserts/updates are 3 separate statements and the @@ROWCOUNT will only reflect the last statement executed.

BEGIN TRANSACTION Script;
GO

INSERT INTO TableA (id) VALUES (1)
INSERT INTO TableB (id) VALUES (1)
UPDATE TableC SET id=1 WHERE id=2
GO

IF (@@ROWCOUNT=3 AND @@ERROR=0)
    BEGIN
    COMMIT
    END
ELSE
    BEGIN
    PRINT 'Error: Rolling back transaction'
    ROLLBACK TRANSACTION Script
    END
GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:36:19+00:00Added an answer on June 2, 2026 at 4:36 pm

    If you put SET XACT_ABORT ON before you start transaction, in case of an error, rollback will be issued automatically.

    SET XACT_ABORT ON
    
    begin transaction
    
    INSERT INTO TableA (id) VALUES (1)
    INSERT INTO TableB (id) VALUES (1)
    UPDATE TableC SET id=1 WHERE id=2
    
    commit transaction
    

    If you want to do rollback yourself, use try .. catch block.

    begin transaction
    
    begin try
    
      INSERT INTO TableA (id) VALUES (1)
      INSERT INTO TableB (id) VALUES (1)
      UPDATE TableC SET id=1 WHERE id=2
    
      commit transaction
    
    end try
    
    begin catch
      raiserror('Message here', 16, 1)
      rollback transaction
    end catch
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a proper way yet to unit test views with the aspx view
What's the proper way to initialize a class_attribute in Rails? I am using this:
a function can return null , but is there any way for the new
What's the proper way to force an RSpec test to fail? I'm considering 1.should
What would be the proper way to unit test an abstract class which takes
In Django URL patterns, what's the proper way to take this URL: http://example/i/fb/#access_token=12345678910|b1827b912847b81938747b91849-193867192305817|EKWOGJhcinWIjWij8174-NgjRojb&expires_in=0 And
What is the proper way to indicate something like an example given a list
What is the most proper way to sending email of minimal 1000 or more
What's the proper way to convert from a scientific notation string such as 1.234567E-06
What is the proper way to minimize a WinForms app to the system tray?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.