Sql oracle中发生错误时如何回滚tran

Sql oracle中发生错误时如何回滚tran,sql,oracle,plsql,Sql,Oracle,Plsql,我尝试解决下面的两个请求,但我只为第一个请求编写代码,, 有谁能帮助解决第二个问题吗 使用游标编写SQL语句,根据更新更新所有员工的工资 条件句 确保所有更新记录都已完全更新。数据库 如果任何更新失败,事务将回滚。 oracle中发生错误时如何回滚事务 不要使用COMMIT或任何DDL语句,如果出现异常,事务将自动回滚 如果提交,则事务只能回滚到提交,而不能回滚到事务的开始。如果使用DDL语句,那么它将发出隐式提交 oracle中发生错误时如何回滚事务 不要使用COMMIT或任何DDL语句,如果

我尝试解决下面的两个请求,但我只为第一个请求编写代码,, 有谁能帮助解决第二个问题吗

使用游标编写SQL语句,根据更新更新所有员工的工资 条件句 确保所有更新记录都已完全更新。数据库 如果任何更新失败,事务将回滚。 oracle中发生错误时如何回滚事务

不要使用COMMIT或任何DDL语句,如果出现异常,事务将自动回滚

如果提交,则事务只能回滚到提交,而不能回滚到事务的开始。如果使用DDL语句,那么它将发出隐式提交

oracle中发生错误时如何回滚事务

不要使用COMMIT或任何DDL语句,如果出现异常,事务将自动回滚

如果提交,则事务只能回滚到提交,而不能回滚到事务的开始。如果使用DDL语句,它将发出隐式提交。

试试这个

declare
cur_empno int;
cursor salary_increment  IS
select employee.empno from EMPLOYEE 
where emplevel in(
    select EmpNo from EMP_SKILL
    group by EmpNo
    having count(*)>1
);
begin
open salary_increment;
loop
fetch salary_increment into cur_empno;
exit when salary_increment%notfound;
update EMPLOYEE set Salary=Salary+300000 
where EmpNo=cur_empno and empLevel=2 and add_months(startdate,24)<=current_date;
update EMPLOYEE set Salary=Salary+500000 
where EmpNo=cur_empno and empLevel=3 and add_months(startdate,36)<=current_date;
end loop;
close salary_increment;

EXCEPTION
    WHEN OTHERS THEN
    ROLLBACK;

COMMIT;
END;
试试这个

declare
cur_empno int;
cursor salary_increment  IS
select employee.empno from EMPLOYEE 
where emplevel in(
    select EmpNo from EMP_SKILL
    group by EmpNo
    having count(*)>1
);
begin
open salary_increment;
loop
fetch salary_increment into cur_empno;
exit when salary_increment%notfound;
update EMPLOYEE set Salary=Salary+300000 
where EmpNo=cur_empno and empLevel=2 and add_months(startdate,24)<=current_date;
update EMPLOYEE set Salary=Salary+500000 
where EmpNo=cur_empno and empLevel=3 and add_months(startdate,36)<=current_date;
end loop;
close salary_increment;

EXCEPTION
    WHEN OTHERS THEN
    ROLLBACK;

COMMIT;
END;

您的代码几乎没有问题。我想你应该试试下面的代码-

DECALRE
cur_empno int;
cursor salary_increment  IS
select employee.empno
  from EMPLOYEE 
 where emplevel in(select EmpNo from EMP_SKILL        -- Column name should be Emplevel instead of Empno.
                    group by EmpNo
                   having count(*)>1);
BEGIN
     OPEN salary_increment;
     LOOP
         FETCH salary_increment INTO cur_empno;
         EXIT WHEN salary_increment%notfound;
         
         -- Use Single Update column instead of multiple update statement.
         update EMPLOYEE
            set Salary= CASE WHEN empLevel=2 AND add_months(startdate,24)<=current_date 
                                  THEN Salary + 300000 
                             WHEN empLevel=3 AND add_months(startdate,36)<=current_date
                                  THEN Salary + 500000
          where EmpNo=cur_empno
            and empLevel IN (2, 3)
            and add_months(startdate,36)<=current_date;

     END LOOP;
     CLOSE salary_increment;
     -- Use 1 commit at the end of transaction.
     COMMIT;
EXCEPTION
    -- Rollback the transaction if there is any issue.
    WHEN OTHERS THEN
    ROLLBACK;

END;

您的代码几乎没有问题。我想你应该试试下面的代码-

DECALRE
cur_empno int;
cursor salary_increment  IS
select employee.empno
  from EMPLOYEE 
 where emplevel in(select EmpNo from EMP_SKILL        -- Column name should be Emplevel instead of Empno.
                    group by EmpNo
                   having count(*)>1);
BEGIN
     OPEN salary_increment;
     LOOP
         FETCH salary_increment INTO cur_empno;
         EXIT WHEN salary_increment%notfound;
         
         -- Use Single Update column instead of multiple update statement.
         update EMPLOYEE
            set Salary= CASE WHEN empLevel=2 AND add_months(startdate,24)<=current_date 
                                  THEN Salary + 300000 
                             WHEN empLevel=3 AND add_months(startdate,36)<=current_date
                                  THEN Salary + 500000
          where EmpNo=cur_empno
            and empLevel IN (2, 3)
            and add_months(startdate,36)<=current_date;

     END LOOP;
     CLOSE salary_increment;
     -- Use 1 commit at the end of transaction.
     COMMIT;
EXCEPTION
    -- Rollback the transaction if there is any issue.
    WHEN OTHERS THEN
    ROLLBACK;

END;

我认为一旦失败,它就会卷土重来。不是吗?我想它已经会在失败后卷土重来了。是吗?谢谢你。但是什么是别人和ABC的例外呢?当别人将所有未提及的例外都套住的时候。在这种情况下,一切都是例外。到ABC就是回滚到一个保存点ABC。在这种情况下,你不需要这个。我已经改变了我的答案。谢谢。但是什么是别人和ABC的例外呢?当别人将所有未提及的例外都套住的时候。在这种情况下,一切都是例外。到ABC就是回滚到一个保存点ABC。在这种情况下,你不需要这个。我改变了答案。哇,非常感谢:哇,非常感谢: