对sql中的delete命令使用回滚查询



我必须在生产数据库中运行删除和更新查询,where条件太复杂,无法应用。我已经创建了查询,但在执行之前,我想测试这个查询。所以我想在应用实际的删除查询之前回滚查询。如何做到这一点?

使用BEGINTRANS启动事务,然后执行查询。

然后查看查询的结果,如果它们是您想要的,那么您可以提交事务,或者使用ROLLBACK命令将其回滚。

这里有一个例子:

BEGIN TRAN t1 -- Begin the transaction
DELETE * FROM Table_Name WHERE col=1  -- Do the "scary operation" in 
--the production environment
SELECT * FROM Table_Name -- Make sure it did what you thought it should 
have.
-- Then depending on results: 
COMMIT TRAN t1 -- Satisfied with results - Commits the transaction (delete 
operation)
ROLLBACK TRAN t1 -- Results not as expected - Rollback the transaction.

最新更新