是否有可能捕获触发重复异常的主键或记录rowID?
表1具有PK:col1和Unique1:col2
例如
begin
insert into table1(col1, col2, col3)
values (1, 2, 3);
exception
when dup_val_on_index then
--- here, can you somehow indicate either PK or ROWID of the record that generated the exception of uniqueness?
e.g.
update table1 set
col3 = 100
where rowid = "GETROWID" or col1 = "GETPK";
end;
在“普通”代码中,您不使用常量来插入值;您通常会将值包含在变量中,因此您的代码应更像:
DECLARE
strVar1 TABLE1%TYPE;
nVar2 NUMBER;
nVar3 NUMBER;
begin
SELECT s1, n2, n3
INTO strVar1, nVar2, nVar3
FROM SOME_TABLE;
insert into table1(col1, col2, col3)
values (strVar1, nVar2, nVar3);
exception
when dup_val_on_index then
update table1
set col3 = 100
where col1 = strVar1;
end;
但是更好的主意是首先使用MERGE语句来避免异常:
MERGE INTO TABLE1 t1
USING (SELECT S1, N2, N3
FROM SOME_TABLE) s
ON (t1.COL1 = s.S1)
WHEN MATCHED THEN
UPDATE SET COL3 = 100
WHEN NOT MATCHED THEN
INSERT (COL1, COL2, COl3)
VALUES (s.S1, s.N2, s.N3);
LOG ERRORS INTO可以在这里提供帮助。使用此子句之前,您必须准备一个错误表:
begin
dbms_errlog.create_error_log('table1');
end;
将创建err $ _table1表。现在使用其他功能运行插入]
insert into table1(col1, col2, col3) values (1, 2, 3)
log errors into err$_table1 ('some_tag_to_look_for') reject limit unlimited;
使用以下命令查询err $ _table1选择* from err $ _table1;
不会为您提供rowid(仅填充更新和删除行),但是您将获得导致错误和违反索引的确切列值。
如果还不够,您可以在这里找到索引列
select * from all_ind_columns where owner='OWNER_NAME_from_error_table' and index_name = 'Name_from_err_table';
因此,您将知道目标表中的哪一列已经具有您要插入的值->这就是查找行ID或所需内容的方式