Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : Rollback without BEGIN TRANSACTION

Is there a way we can rollback to previous state of the transaction using ROLLBACK without BEGIN TRANSACTION?

delete from table1;
ROLLBACK

Message:

The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.

Any input would be of great help.

Thanks !!!

like image 829
user1993412 Avatar asked Sep 02 '26 23:09

user1993412


1 Answers

To expand on gerrytans answer when you explicitly set IMPLICIT_TRANSACTIONS ON, you can use a ROLLBACK. See the MSDN doco related to this. Note that this isn't the default autocommit transaction mode.

This allows me to run a statement like;

SET IMPLICIT_TRANSACTIONS ON

INSERT INTO my_table (item_type, start_date_time)
VALUES ('TEST', CURRENT_TIMESTAMP)

ROLLBACK

-- Shouldn't return the 'TEST' value inserted above.
SELECT * FROM my_table ORDER BY start_date_time DESC 
like image 137
Mr Moose Avatar answered Sep 05 '26 14:09

Mr Moose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!