ffind 有错误记录的字段名
Ffind field name of record that has error
我有如下的 plsql:
DECLARE
CURSOR c_data IS
SELECT t2.ID, t2.NA, t2.NAME
FROM SABAPAY_TEMP.TEST2 t2;
BEGIN
FOR i IN c_data
LOOP
BEGIN
INSERT INTO SABAPAY_TEMP.TEST t (t.ID, t.NA, t.NAME)
VALUES (i.id, i.na, i.name);
EXCEPTION
WHEN OTHERS
THEN
INSERT INTO SABAPAY_TEMP.TEST5 t5 (t5.VALUE)
VALUES (i.na);
END;
END LOOP;
END;
如您所见,每条有错误且无法插入 table 测试的记录将被插入 'test5' table
我在 'test5' 中有一个字段可以存储字符串。
我想存储有错误的记录的文件名。
我怎样才能找到错误的原因?
也许在 test5
中存储错误消息 sqlerrm?:
declare
cursor c_data is select id, na, name from test2;
v_err varchar2(1000);
begin
for i in c_data loop
begin
insert into test (id, na, name) values (i.id, i.na, i.name);
exception when others then
v_err := sqlerrm;
insert into test5 (value, reason) values (i.na, v_err);
end;
end loop;
end;
在我的示例中,name
上的约束 not null
被违反,您可以在 reason
列中看到它。
您可以使用 DML 错误日志记录来捕获大多数问题:
create table t (
c1 int
primary key
);
exec DBMS_ERRLOG.CREATE_ERROR_LOG('t', 'err$_t');
insert into t
select 1 from dual
union all
select level from dual
connect by level <= 5
log errors
reject limit unlimited;
select * from t;
C1
1
2
3
4
5
select * from err$_t;
ORA_ERR_NUMBER$ ORA_ERR_MESG$ ORA_ERR_ROWID$ ORA_ERR_OPTYP$ ORA_ERR_TAG$ C1
1 ORA-00001: unique constraint (CHRIS.SYS_C0013611) violated <null> I <null> 1
因此您可以将代码简化为:
BEGIN
INSERT INTO SABAPAY_TEMP.TEST t (t.ID, t.NA, t.NAME)
SELECT t2.ID, t2.NA, t2.NAME
FROM SABAPAY_TEMP.TEST2 t2
LOG ERRORS
REJECT LIMIT UNLIMITED;
END;
/
我有如下的 plsql:
DECLARE
CURSOR c_data IS
SELECT t2.ID, t2.NA, t2.NAME
FROM SABAPAY_TEMP.TEST2 t2;
BEGIN
FOR i IN c_data
LOOP
BEGIN
INSERT INTO SABAPAY_TEMP.TEST t (t.ID, t.NA, t.NAME)
VALUES (i.id, i.na, i.name);
EXCEPTION
WHEN OTHERS
THEN
INSERT INTO SABAPAY_TEMP.TEST5 t5 (t5.VALUE)
VALUES (i.na);
END;
END LOOP;
END;
如您所见,每条有错误且无法插入 table 测试的记录将被插入 'test5' table 我在 'test5' 中有一个字段可以存储字符串。 我想存储有错误的记录的文件名。 我怎样才能找到错误的原因?
也许在 test5
中存储错误消息 sqlerrm?:
declare
cursor c_data is select id, na, name from test2;
v_err varchar2(1000);
begin
for i in c_data loop
begin
insert into test (id, na, name) values (i.id, i.na, i.name);
exception when others then
v_err := sqlerrm;
insert into test5 (value, reason) values (i.na, v_err);
end;
end loop;
end;
在我的示例中,name
上的约束 not null
被违反,您可以在 reason
列中看到它。
您可以使用 DML 错误日志记录来捕获大多数问题:
create table t (
c1 int
primary key
);
exec DBMS_ERRLOG.CREATE_ERROR_LOG('t', 'err$_t');
insert into t
select 1 from dual
union all
select level from dual
connect by level <= 5
log errors
reject limit unlimited;
select * from t;
C1
1
2
3
4
5
select * from err$_t;
ORA_ERR_NUMBER$ ORA_ERR_MESG$ ORA_ERR_ROWID$ ORA_ERR_OPTYP$ ORA_ERR_TAG$ C1
1 ORA-00001: unique constraint (CHRIS.SYS_C0013611) violated <null> I <null> 1
因此您可以将代码简化为:
BEGIN
INSERT INTO SABAPAY_TEMP.TEST t (t.ID, t.NA, t.NAME)
SELECT t2.ID, t2.NA, t2.NAME
FROM SABAPAY_TEMP.TEST2 t2
LOG ERRORS
REJECT LIMIT UNLIMITED;
END;
/