插入 +在包中提交

发布于 2024-09-30 10:55:44 字数 697 浏览 3 评论 0原文

编写一个oracle包来实现记录持久化的最佳解决方案是什么? 我总是写这样的东西:

    create or replace
    PACKAGE BODY           "USP_PRICELIST" AS

      PROCEDURE  usp_TABLE1Save 
        (
        pErrorCode              OUT NUMBER,
        pMessage                OUT VARCHAR2,       
        pPARAM1                 IN CHAR,
        pPARAM2             IN CHAR
        )

        IS

    BEGIN

        pErrorCode := 0;

        INSERT INTO TABLE1
          (PARAM1, PARAM2)
        VALUES
          (pPARAM1, pPARAM2);
        EXCEPTION
                 WHEN OTHERS THEN pErrorCode := SQLCODE; pMessage := SQLERRM;

    END usp_TABLE1Save;

END USP_PRICELIST;

我想知道在插入后是否必须提交。

阿尔贝托

what is the best solution to write a oracle package for record persistence?
I've always written something like this:

    create or replace
    PACKAGE BODY           "USP_PRICELIST" AS

      PROCEDURE  usp_TABLE1Save 
        (
        pErrorCode              OUT NUMBER,
        pMessage                OUT VARCHAR2,       
        pPARAM1                 IN CHAR,
        pPARAM2             IN CHAR
        )

        IS

    BEGIN

        pErrorCode := 0;

        INSERT INTO TABLE1
          (PARAM1, PARAM2)
        VALUES
          (pPARAM1, pPARAM2);
        EXCEPTION
                 WHEN OTHERS THEN pErrorCode := SQLCODE; pMessage := SQLERRM;

    END usp_TABLE1Save;

END USP_PRICELIST;

and I was wondering if I have to COMMIT after the INSERT INTO.

Alberto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

睫毛上残留的泪 2024-10-07 10:55:44

不会在过程中进行提交,并将其留给调用该过程的代码。这允许该过程用作较大事务的一部分。插入不是隐式提交的。

I would not put a commit in the procedure, and leave that to the code that calls the procedure. This allows the procedure to be used as part of a larger transaction. The insert is not implicitly committed.

善良天后 2024-10-07 10:55:44

这实际上取决于您是否希望您的操作参与事务或原子操作。

It really depends on whether you want your operation to take part in a transaction or to be atomic.

爱本泡沫多脆弱 2024-10-07 10:55:44

请小心,如果您将提交放在包中,它将提交整个事务

create table testcommit (colA varchar2(50)) ;

DECLARE
  PROCEDURE SELFCOMMIT(VAL IN TESTCOMMIT.COLA%TYPE) AS
                BEGIN
                     INSERT INTO TESTCOMMIT(COLA) VALUES(VAL);
                     COMMIT ;
                END SELFCOMMIT ;
  PROCEDURE NOCOMMIT(VAL IN TESTCOMMIT.COLA%TYPE) AS
                BEGIN
                     INSERT INTO TESTCOMMIT(COLA) VALUES(VAL);
                END NOCOMMIT ;              


BEGIN
  INSERT INTO TESTCOMMIT(COLA) VALUES('INITIAL');
  SELFCOMMIT('FIRST SELF COMMIT');
  ROLLBACK ; --KILL TRANSACTION

  INSERT INTO TESTCOMMIT(COLA) VALUES('SECOND MAIN INSERT');
  NOCOMMIT('NO AUTO COMMIT');
  ROLLBACK;


END ;
/
SELECT * FROM TESTCOMMIT;

-->
COLA                                               
-------------------------------------------------- 
INITIAL                                            
FIRST SELF COMMIT  
-->NOTE THE SELFCOMMIT AFFECTS THE ENTIRE TRANSACTION, THUS RENDERING THE ROLLBACK MOOT

--drop table testcommit;

Be careful, if you place the commit in the package it will commit the entire transaction

create table testcommit (colA varchar2(50)) ;

DECLARE
  PROCEDURE SELFCOMMIT(VAL IN TESTCOMMIT.COLA%TYPE) AS
                BEGIN
                     INSERT INTO TESTCOMMIT(COLA) VALUES(VAL);
                     COMMIT ;
                END SELFCOMMIT ;
  PROCEDURE NOCOMMIT(VAL IN TESTCOMMIT.COLA%TYPE) AS
                BEGIN
                     INSERT INTO TESTCOMMIT(COLA) VALUES(VAL);
                END NOCOMMIT ;              


BEGIN
  INSERT INTO TESTCOMMIT(COLA) VALUES('INITIAL');
  SELFCOMMIT('FIRST SELF COMMIT');
  ROLLBACK ; --KILL TRANSACTION

  INSERT INTO TESTCOMMIT(COLA) VALUES('SECOND MAIN INSERT');
  NOCOMMIT('NO AUTO COMMIT');
  ROLLBACK;


END ;
/
SELECT * FROM TESTCOMMIT;

-->
COLA                                               
-------------------------------------------------- 
INITIAL                                            
FIRST SELF COMMIT  
-->NOTE THE SELFCOMMIT AFFECTS THE ENTIRE TRANSACTION, THUS RENDERING THE ROLLBACK MOOT

--drop table testcommit;
七堇年 2024-10-07 10:55:44

您还应该了解自主事务的概念

You should also look at the concept of autonomous transactions

扎心 2024-10-07 10:55:44

默认情况下,Oracle 没有自动提交,因此您必须这样做。

By default Oracle has no auto-commit, so you have to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文