如何在 Oracle SQL 中使用回滚/提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3908533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto use Rollback/Commit in Oracle SQL
提问by dpsthree
I am trying to utilize transaction functionality in Oracle SQL for the first time and can't seem to find a good explanation. I understand that starting a new session will begin a new transaction. I also understand that commit/rollback is used to end it. What I am trying to do is execute two statements and if I either of them fail, undo any changes they might have made and continue with execution. How can I check for this condition and issue a commit or rollback accordingly?
我第一次尝试在 Oracle SQL 中使用事务功能,但似乎找不到很好的解释。我了解开始新会话将开始新事务。我也明白提交/回滚是用来结束它的。我想要做的是执行两个语句,如果其中一个失败,撤消它们可能所做的任何更改并继续执行。如何检查这种情况并相应地发出提交或回滚?
回答by ObiWanKenobi
Use a PL/SQL block and write something like this:
使用 PL/SQL 块并编写如下内容:
begin
statement_zero;
savepoint my_savepoint;
begin
-- if either of these fail, then exception section will be executed
statement_one;
statement_two;
exception
when others then
rollback to my_savepoint;
end;
statement_three;
commit;
end;
See also http://www.adp-gmbh.ch/ora/concepts/transaction.html
回答by andr
Along with a nice exaplample ObiWanKenobi provded a detailed explanation of Oracle transactions can be found at Chapter 4of Oracle Concepts guide (the link I've provided goes for 10.2, you can find the doc suitable for your version at Oracle website as well). I suggest you read this chapter to understand how Oracle handles transaction management, and the doc at whole is very good piece of information for undestanding how Oracle DB work.
除了一个很好的例子,ObiWanKenobi 还提供了对 Oracle 事务的详细解释,可以在Oracle 概念指南的第 4 章找到(我提供的链接指向 10.2,您也可以在 Oracle 网站上找到适合您的版本的文档)。我建议您阅读本章以了解 Oracle 如何处理事务管理,整个文档对于理解 Oracle DB 的工作方式是非常好的信息。