关于oracle的事务中,如何判断一条语句是否成功执行

2025-02-25 16:13:25
推荐回答(5个)
回答1:

一个简单的正常提交-异常回滚 的例子代码.

SQL> select * from test_main;

ID VALUE
---------- --------------------
2 TWO
3 THREE
1 ONE

SQL> BEGIN
2 -- 插入2条同样的数据,使主键重复,引发错误后回滚事务.
3 INSERT INTO test_main(id, value) VALUES (4, 'FOUR');
4 INSERT INTO test_main(id, value) VALUES (4, 'FOUR');
5 COMMIT;
6 EXCEPTION
7 WHEN OTHERS THEN
8 dbms_output.put_line('Error Code = ' || TO_CHAR(SQLCODE) );
9 dbms_output.put_line('Error Message = ' || SQLERRM );
10 -- 回滚事务
11 ROLLBACK;
12 END;
13 /

PL/SQL procedure successfully completed.

SQL> select * from test_main;

ID VALUE
---------- --------------------
2 TWO
3 THREE
1 ONE

SQL>

回答2:

oracle判断是否执行成功
begin
insert into ...
-- rowcount 表示影响的行数,如果为0表示没有成功插入
if sql%rowcount > 0 then
commit;
else
rollback;
end if;
end;

回答3:

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERR : ' || TO_CHAR(SQLERRM));
ROLLBACK;
END;
这个只是抓异常的,你这个问题也没有研修过,知道怎么做的时候,能把方法公布一下不?

回答4:

begin
insert into xxxxxxx;
insert into xxxxxxx;
commit;
end;

回答5:

begin
insert into xxxxxxx;
insert into xxxxxxx;
commit;
exception when others then
rollback;
end;