Link to home
Start Free TrialLog in
Avatar of earwig75
earwig75

asked on

A question about SQL Transactions

I have a couple queries wrapped in an SQL transaction like below. If one or more of the sql statements fails or doesn't complete properly, I'd like to rollback everything automatically. Is this possible? Thank you.

BEGIN TRANSACTION;

INSERT INTO.....

UPDATE tblBlah...

INSERT INTO....

COMMIT TRANSACTION;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of earwig75
earwig75

ASKER

@Jim Horn, would I need to add anything to the catch, other than the rollback? Does the catch automatically run if an error is thrown?

@Bill Prew, thank you! Both of these seem great.
>would I need to add anything to the catch, other than the rollback?
No, other than to figure out a graceful way to report back to the calling code that the overall transaction was rolled back so it doesn't assume that the transaction was successfully committed.