简体   繁体   English

如何在oracle SQL中使用事务?

[英]How do I use transaction with oracle SQL?

I am trying to use transaction blocks on a SQL-Console with an Oracle DB. 我试图在带有Oracle DB的SQL控制台上使用事务块。 I'm used to use transaxction blocks in PostgreSQL like 我习惯在PostgreSQL中使用transaxction块

BEGIN;
<simple sql statement>
END;

but in oracle it seems that this is not possible. 但在oracle中似乎这是不可能的。 I'm always getting "ORA-00900" errors and I don't know how to fix that. 我总是得到“ORA-00900”错误,我不知道如何解决这个问题。 If I just use SQL-Statements like 如果我只使用像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. 但它仍然会抛出一个ORA-00900。 My operating system is windows, I am using IntelliJ IDEA and a Oracle 11g DB. 我的操作系统是windows,我使用的是IntelliJ IDEA和Oracle 11g DB。

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 transaction then, 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

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. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE - 在这种情况下,读取一致性将从执行此命令开始。 READ ONLY transaction has the same read consistency effect. READ ONLY事务具有相同的读取一致性效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM