为什么我们在MS SQL服务器中编写存储的进程时使用rollback语句? [英] Why do we use rollback statement during writing a stored proceedure in MS SQL server?

查看:70
本文介绍了为什么我们在MS SQL服务器中编写存储的进程时使用rollback语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问有人在MS SQL Server中的存储过程中解释Rollback语句吗?如果我们不使用这个声明,会发生什么?使用它有什么好处?

谢谢。



我的尝试:



我正在尝试与高级开发人员和其他在线资源进行讨论。

Would you please someone explain the Rollback statement in Stored Procedure in MS SQL Server? What happen,if we do not use this statement? And what are the benefits of using it?
Thanks.

What I have tried:

I am trying to discuss with senior developer and other online resources.

推荐答案

ROLLBACK是交易的一部分,它用于抛弃自上一次BEGIN TRANS语句以来对DB的所有更改。从事务开始到回滚或提交,更改不会直接转到实时数据库,而是存储为待处理以供日后使用。



如果(例如)您正在向两个相关表添加数据,并且任何更新失败,您将使用ROLLBACK放弃对两个表的所有更改,以便不留下不完整或误导信息。如果它们都成功,则使用COMMIT来实际更新真实表,以便其他用户可以访问数据。
ROLLBACK is part of transactions, and it's used to "throw away" all changes to the DB since the last BEGIN TRANS statement. From the start of the transaction to a rollback or commit, changes do no go directly to the "live" DB but are stored as "pending" for later.

If (for example) you are adding data to two related tables and any update fails, you would use ROLLBACK to discard all changes to both tables so that "incomplete" or "misleading" information isn't left behind. If they all succeed, you use COMMIT instead to actually update the "real tables" so other users have access to the data.


回滚通常在if(错误)块内。



这个想法是你可以将事务包装到事务块中,其中所有事务都成功或全部失败。最糟糕的情况是在存储过程中最后一个插入失败时有一些插入成功。这真的搞砸了你的数据。



如果回滚不在if(错误)块中,那么我作者可能更改一些数据,获取它们让它回滚,但这将是非常糟糕的做法
The rollback is usually within an if(error) block.

The idea is that you can wrap transactions up into transaction blocks where either all succeed or all fail. The worst situation is to have a few inserts succeed where the last one fails in a stored proc. It can really mess up your data.

If the rollback is not in an if(error) block then I guess that the author might change some data, fetch it them let it roll back but that would be very bad practice


这篇关于为什么我们在MS SQL服务器中编写存储的进程时使用rollback语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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