如何在 oracle 数据库中运行查询集如果在查询中任何获取失败必须回滚?

问题描述

我知道这个问题可能会重复,但我被提出了同样的问题,因为我的要求是不同的,而且是 ORACLE 数据库的新要求。

基本上,我有四个不同表的查询

查询 1:表名:users

INSERT INTO users (user_id,user_name,acc_is_active,full_name,email,description,us_report_role,us_is_system,us_is_active)
  VALUES ('aaaaaa',' araut_xxxxxxxx’','Y',‘Ram G DON’,‘example@gmail.com’,‘Staff_ID','0','N','Y')

查询 2:表名:序列

update sequences set SQ_SEQ_VALUE='aaaaaa' where SQ_SEQ_NAME='USER_SEQ'

查询 3:表名:users_auth_data

 INSERT into users_auth_data (UAD_USER_ID,UAD_KEY,UAD_VALUE) VALUES ('aaaaaa','LDAP_SERVER_ID','-1')

查询 4:表:users_auth_data

INSERT into users_auth_data (UAD_USER_ID,'LDAP_USER_DN','CN=Staff_ID,OU=ExamplePeople,DC=InfoDir,DC=DEV,DC=EXAMPLE')

这是我需要按顺序运行的四个查询。如果其中任何查询失败,那么我们必须回滚所有更改。如果所有查询都成功运行,则将提交所有查询更改。

为了实现上述场景,我们需要遵循哪种方法

如果有人帮我解决问题,那可能会有很大帮助。

解决方法

您正在寻找的是交易。

通过使用异常的 Pl/Sql 块执行此操作的一种方法。 确保您设置

自动提交关闭;

Declare 
<declare your variables>

Begin
<your queries>
commit; --This will commit the changes if all queries run fine

Exception
WHEN OTHERS THEN

rollback; --This will rollback the changes if there are any exceptions caught.


end;

还有另一种方法来实现这一点,那就是使用保存点。 这在下面的链接中有更好的描述:

https://www.tutorialspoint.com/plsql/plsql_transactions.htm