在执行时期,如果我再次执行执行,则添加了10个

发布于 2025-02-12 09:10:26 字数 1700 浏览 0 评论 0原文

问题:编写一个过程以在表中插入记录,其中empno是主要键。如果我们插入任何重复empno,我们显示错误

违反了唯一的价值约束

,值10将添加到empno中,并将数据插入表中。

如果发生任何其他错误,请通过异常处理错误。

代码:

CREATE OR REPLACE PROCEDURE check_duplicate_row IS
    PRAGMA autonomous_transaction;
    CURSOR table1_data IS
    SELECT
        empno,
        empname
    FROM
        employee_details;

    employee_record table1_data%rowtype;
BEGIN
    FOR employee_record IN table1_data LOOP
        BEGIN
            INSERT INTO emp_target (
                empno,
                empname
            ) VALUES (
                employee_record.empno,
                employee_record.empname
            );

            EXIT WHEN table1_data%notfound;
            COMMIT;
        EXCEPTION
            WHEN dup_val_on_index THEN
                INSERT INTO emp_target (
                    empno,
                    empname
                )
                    SELECT
                        empno + 10,
                        empname
                    FROM
                        employee_details;

                COMMIT;
        END;
    END LOOP;
END;
/

错误从行开始:57命令 -
开始check_duplicate_row;结束;

错误报告:

ora-00001:唯一约束(adwhs.emp_target_pk)违反了
ORA-06512:在“ adwhs.check_duplate_row”,第20行
ORA-06512:在“ adwhs.check_duplate_row”,第20行
ORA-00001:唯一约束(adwhs.emp_target_pk)违反了
ORA-06512:在“ adwhs.check_duplate_row”,第14行
ORA-06512:在第1行

  1. 00000-“违反了唯一约束(%s。s)”
    原因:更新或插入语句尝试插入重复键。
    对于在DBMS Mac模式下配置的受信任的Oracle,您可以看到此消息,如果存在重复的条目。
    操作:删除唯一限制或不插入密钥。

Question: write a procedure to insert record in table where EMPNO is the primary key. If we insert any duplicate EMPNO, we show an error

unique value constraint violated

In the place of duplicate, value 10 will be added to the EMPNO and insert the data into the table.

And if any other errors occur, handle the error through an exception.

Code:

CREATE OR REPLACE PROCEDURE check_duplicate_row IS
    PRAGMA autonomous_transaction;
    CURSOR table1_data IS
    SELECT
        empno,
        empname
    FROM
        employee_details;

    employee_record table1_data%rowtype;
BEGIN
    FOR employee_record IN table1_data LOOP
        BEGIN
            INSERT INTO emp_target (
                empno,
                empname
            ) VALUES (
                employee_record.empno,
                employee_record.empname
            );

            EXIT WHEN table1_data%notfound;
            COMMIT;
        EXCEPTION
            WHEN dup_val_on_index THEN
                INSERT INTO emp_target (
                    empno,
                    empname
                )
                    SELECT
                        empno + 10,
                        empname
                    FROM
                        employee_details;

                COMMIT;
        END;
    END LOOP;
END;
/

Error starting at line : 57 in command -
BEGIN check_duplicate_row; END;

Error report:

ORA-00001: unique constraint (ADWHS.EMP_TARGET_PK) violated
ORA-06512: at "ADWHS.CHECK_DUPLICATE_ROW", line 20
ORA-06512: at "ADWHS.CHECK_DUPLICATE_ROW", line 20
ORA-00001: unique constraint (ADWHS.EMP_TARGET_PK) violated
ORA-06512: at "ADWHS.CHECK_DUPLICATE_ROW", line 14
ORA-06512: at line 1

  1. 00000 - "unique constraint (%s.%s) violated"
    Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
    For Trusted Oracle configured in DBMS MAC mode, you may see this message if a duplicate entry exists at a different level.
    Action: Either remove the unique restriction or do not insert the key.

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

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

发布评论

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

评论(1

薄荷港 2025-02-19 09:10:26

我对一些名称和数据(修复值)进行了一些更改,但是您在代码中遇到了一些问题。
您的第二次尝试(在例外)编写+10 empno的记录没有Propper PL/SQL块,而异常零件可以处理错误。在第一个块中,异常无法处理可能的其他错误。将其采用到您的表/列名和实际数据中,看看第一次运行时会发生什么以及当您再次运行时会发生什么……

create or replace PROCEDURE check_duplicate_row IS
    PRAGMA autonomous_transaction;
    CURSOR table1_data IS
    SELECT
        7900 as empno,
        'SOME_NAME' as empname
    FROM
        dual;

    employee_record table1_data%rowtype;
BEGIN
    FOR employee_record IN table1_data LOOP
        BEGIN
            INSERT INTO emp (
                empno,
                ename
            ) VALUES (
                employee_record.empno,
                employee_record.empname
            );

            EXIT WHEN table1_data%notfound;
            COMMIT;
        EXCEPTION
            WHEN dup_val_on_index THEN
                DBMS_OUTPUT.PUT_LINE('DUP_VAL_ON_INDEX exception raised --> Trying again with empno+10...');
                BEGIN
                    INSERT INTO emp (
                        empno,
                        ename
                    )
                        SELECT
                            7900 + 10 as empno,
                            'SOME_NAME' as empname
                        FROM
                            dual;
                    COMMIT;
                    DBMS_OUTPUT.PUT_LINE('Succes with empno+10...');
                EXCEPTION
                    WHEN dup_val_on_index THEN
                        DBMS_OUTPUT.PUT_LINE('DUP_VAL_ON_INDEX exception raised again --> second attempt failed   --> getting out ...');
                    WHEN OTHERS THEN
                        DBMS_OUTPUT.PUT_LINE(SQLERRM);
                END;
            WHEN OTHERS THEN
                  DBMS_OUTPUT.PUT_LINE(SQLERRM);
        END;
    END LOOP;
END;

应该是这样的:

SET SERVEROUTPUT ON
Begin
    CHECK_DUPLICATE_ROW();
End;
--  R e s u l t   1st run
--  anonymous block completed
--  DUP_VAL_ON_INDEX exception raised --> Trying again with empno+10...
--  Succes with empno+10...
--  
--  R e s u l t   2nd run
--  anonymous block completed
--  DUP_VAL_ON_INDEX exception raised --> Trying again with empno+10...
--  DUP_VAL_ON_INDEX exception raised again --> second attempt failed   --> getting out ...

此外,该过程的名称是误导性的实际上,它是一个插入记录过程,而不是检查重复的一个。它做了一些例外处理重复项,但基本上是插入过程。问候...

I put some changes with some names and data (fix values) but you have a few issues in the code.
Your second attempt (within Exception) to write a record with +10 empno does not have a propper pl/sql block with an exception part to handle the error. And in the first block exception does not handle possible other errors. Adopt this to your table/column names and to your actual data and see what happens when you run it first time and what when you run it again...

create or replace PROCEDURE check_duplicate_row IS
    PRAGMA autonomous_transaction;
    CURSOR table1_data IS
    SELECT
        7900 as empno,
        'SOME_NAME' as empname
    FROM
        dual;

    employee_record table1_data%rowtype;
BEGIN
    FOR employee_record IN table1_data LOOP
        BEGIN
            INSERT INTO emp (
                empno,
                ename
            ) VALUES (
                employee_record.empno,
                employee_record.empname
            );

            EXIT WHEN table1_data%notfound;
            COMMIT;
        EXCEPTION
            WHEN dup_val_on_index THEN
                DBMS_OUTPUT.PUT_LINE('DUP_VAL_ON_INDEX exception raised --> Trying again with empno+10...');
                BEGIN
                    INSERT INTO emp (
                        empno,
                        ename
                    )
                        SELECT
                            7900 + 10 as empno,
                            'SOME_NAME' as empname
                        FROM
                            dual;
                    COMMIT;
                    DBMS_OUTPUT.PUT_LINE('Succes with empno+10...');
                EXCEPTION
                    WHEN dup_val_on_index THEN
                        DBMS_OUTPUT.PUT_LINE('DUP_VAL_ON_INDEX exception raised again --> second attempt failed   --> getting out ...');
                    WHEN OTHERS THEN
                        DBMS_OUTPUT.PUT_LINE(SQLERRM);
                END;
            WHEN OTHERS THEN
                  DBMS_OUTPUT.PUT_LINE(SQLERRM);
        END;
    END LOOP;
END;

It should be something like this:

SET SERVEROUTPUT ON
Begin
    CHECK_DUPLICATE_ROW();
End;
--  R e s u l t   1st run
--  anonymous block completed
--  DUP_VAL_ON_INDEX exception raised --> Trying again with empno+10...
--  Succes with empno+10...
--  
--  R e s u l t   2nd run
--  anonymous block completed
--  DUP_VAL_ON_INDEX exception raised --> Trying again with empno+10...
--  DUP_VAL_ON_INDEX exception raised again --> second attempt failed   --> getting out ...

Also, the name of the procedure is missleading as it is, actually, an insert record procedure not check duplicates one. It does some exception handling of the duplicates, but basically it is an insert procedure. Regards...

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