将事务放入存储过程以取消所有操作

时间:2017-04-14 09:59:48

标签: oracle plsql

我有oracles存储过程,执行2次操作,删除然后更新。我的目标是更新应该总是更新一行,如果更多行更新,我想取消两个操作,这是我试过的交易

BEGIN

  BEGIN transaction trans;
     (here is delete query)


      (here is update query)
     IF SQL%ROWCOUNT > 1 THEN
            ROLLBACK TO tran;
    ELSE
          COMMIT;
    END IF;
  END;

END;

但这会产生错误Error(5,23): PLS-00103: Encountered the symbol "TRANS" when expecting one of the following: := . ( @ % ; The symbol ":=" was substituted for "TRANS" to continue.

什么是不正确的

2 个答案:

答案 0 :(得分:2)

在Oracle中你必须使用SAVEPOINTS,应该是这样的:

BEGIN 

SAVEPOINT tran;
     (here is delete query)


      (here is update query)
     IF SQL%ROWCOUNT > 1 THEN
            ROLLBACK TO tran;
    ELSE
          COMMIT;
    END IF;
END;

但是,对我而言,目前尚不清楚您要回滚的位置,SAVEPOINT tran;可能应该(here is delete query)之后 - 根据您的要求而定。

答案 1 :(得分:1)

尝试:

 BEGIN
        (here is delete query)

        (here is update query)
        IF SQL%ROWCOUNT > 1 THEN
            RAISE_APPLICATION_ERROR(-20001, 'rollbacked');
        END IF;
 END;

oracle中的存储过程默认是事务性的,如果你有2个查询(删除和更新)并且你想要中止它们,不需要额外的BEGIN transaction ...或类似的东西