在执行时期,如果我再次执行执行,则添加了10个
问题:编写一个过程以在表中插入记录,其中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行
- 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
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对一些名称和数据(修复值)进行了一些更改,但是您在代码中遇到了一些问题。
您的第二次尝试(在例外)编写+10 empno的记录没有Propper PL/SQL块,而异常零件可以处理错误。在第一个块中,异常无法处理可能的其他错误。将其采用到您的表/列名和实际数据中,看看第一次运行时会发生什么以及当您再次运行时会发生什么……
应该是这样的:
此外,该过程的名称是误导性的实际上,它是一个插入记录过程,而不是检查重复的一个。它做了一些例外处理重复项,但基本上是插入过程。问候...
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...
It should be something like this:
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...