如何在 oracle SQL 中使用事务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35178808/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:24:08  来源:igfitidea点击:

How do I use transaction with oracle SQL?

sqloracletransactions

提问by Vortilion

I am trying to use transaction blocks on a SQL-Console with an Oracle DB. I'm used to use transaxction blocks in PostgreSQL like

我正在尝试在带有 Oracle DB 的 SQL 控制台上使用事务块。我习惯于在 PostgreSQL 中使用事务块,例如

BEGIN;
<simple sql statement>
END;

but in oracle it seems that this is not possible. I'm always getting "ORA-00900" errors and I don't know how to fix that. If I just use SQL-Statements like

但在 oracle 中,这似乎是不可能的。我总是收到“ORA-00900”错误,我不知道如何解决。如果我只使用 SQL 语句,例如

<simple sql statement>
COMMIT;

it works. But isn't there some tag to define the start of a transaction? I tried

有用。但是不是有一些标签来定义事务的开始吗?我试过

START TRANSACTION;
<simple sql statement>
COMMIT;

But it still throws an ORA-00900. My operating system is windows, I am using IntelliJ IDEA and a Oracle 11g DB.

但它仍然抛出一个 ORA-00900。我的操作系统是 Windows,我使用的是 IntelliJ IDEA 和 Oracle 11g DB。

回答by kevinsky

You can have an implicit transaction block by issuing one SQL statement as in

您可以通过发出一条 SQL 语句来拥有一个隐式事务块,如

<simple sql statement>
Commit;

For anonymous blocks or PL/SQL procedures/functions/packages more options are available that you may have seen in Postgres.

对于匿名块或 PL/SQL 过程/函数/包,您可能在 Postgres 中看到了更多可用选项。

If you have several statements that must all succeed or all fall (an atomic transactionthen, from the documentation, you can do:

如果您有几个语句必须全部成功或全部失败(原子事务,然后从文档中,您可以执行以下操作:

DECLARE
   <variable declaration>
BEGIN
   <simple sql statement>
   <simple sql statement>
   <simple sql statement>
   SAVEPOINT do_insert;
   <sql insert statement>
EXCEPTION
   WHEN DUP_VAL_ON_INDEX THEN
      ROLLBACK TO do_insert;
      DBMS_OUTPUT.PUT_LINE('Insert has been rolled back');
END;
--and commit outside the transaction

回答by Husqvik

Normal, read committed transaction, starts automatically with the first modified row.

正常的读提交事务,从第一个修改的行自动启动。

If you want to set the transaction explicitly use:

如果要显式设置事务,请使用:

SET TRANSACTION ISOLATION LEVEL READ COMMITTED- but the transaction will be physically created when first row is modified, not when this statement is executed.

SET TRANSACTION ISOLATION LEVEL READ COMMITTED- 但事务将在修改第一行时物理创建,而不是在执行此语句时。

or

或者

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE- in this case read consistency will be as of this command is executed. READ ONLYtransaction has the same read consistency effect.

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE- 在这种情况下,读取一致性将在执行此命令时生效。READ ONLY事务具有相同的读一致性效果。