SQL Server:在没有开始事务的情况下回滚 [英] SQL Server : Rollback without BEGIN TRANSACTION

查看:37
本文介绍了SQL Server:在没有开始事务的情况下回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在没有 BEGIN TRANSACTION 的情况下使用 ROLLBACK 回滚到事务的先前状态?

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

delete from table1;
ROLLBACK

留言:

ROLLBACK TRANSACTION 请求没有对应的 BEGIN TRANSACTION.

任何输入都会有很大帮助.

Any input would be of great help.

谢谢!!!

推荐答案

要在显式设置 IMPLICIT_TRANSACTIONS ON 时扩展 gerrytans 的答案,可以使用 ROLLBACK.请参阅与此相关的 MSDN 文档.请注意,这不是默认的 自动提交事务模式.

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 

这篇关于SQL Server:在没有开始事务的情况下回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆