如何在SQL*PLUS中从批量插入中查找失败的insert语句

如何在SQL*PLUS中从批量插入中查找失败的insert语句,sql,oracle,sql-insert,sqlplus,bulkinsert,Sql,Oracle,Sql Insert,Sqlplus,Bulkinsert,我一直试图通过SQL*PLUS在表中插入近10K行。然而,当我算上SQLDeveloper时,在10K行中有近10行丢失 如何查找无法插入到表中的行 我已经准备好在Excel中插入脚本,从Excel中复制10K行并粘贴到SQL*PLUS中。此过程在5分钟内完成,但缺少一些行 请帮我找到那些缺少的行。这个问题很模糊,我在猜测您的脚本的确切形式,因此我的答案试图为您提供一些选项,但并不精确 Here is one way to find missing rows , mostly table wil

我一直试图通过SQL*PLUS在表中插入近10K行。然而,当我算上SQLDeveloper时,在10K行中有近10行丢失

如何查找无法插入到表中的行

我已经准备好在Excel中插入脚本,从Excel中复制10K行并粘贴到SQL*PLUS中。此过程在5分钟内完成,但缺少一些行


请帮我找到那些缺少的行。

这个问题很模糊,我在猜测您的脚本的确切形式,因此我的答案试图为您提供一些选项,但并不精确

Here is one way to find missing rows , mostly table will have one primary key or make primary key using sequence ... try to load primary key alone in another table say lookup table  .... Then make query between actual loaded table and lookup table to find missing rows

create table temp
( id number,
sal number)

begin 
insert into temp values (1,100);
insert into temp values (2,200);
insert into temp values (3,300);
insert into temp values (5,400);
end;

select * from temp;

create table lookup
(id number);

begin 
insert into lookup values(1);
insert into lookup values(2);
insert into lookup values(3);
insert into lookup values(4);
insert into lookup values(5);
end;

select * from lookup;

select * from lookup where id not in ( select id from temp);
您可以尝试以下方法:

WHENEVER SQLERROR EXIT FAILURE COMMIT
INSERT INTO t1 VALUES ( 'a', 'b', 'c');
INSERT INTO t1 VALUES ( 'a1', 'b1', 'c1');
.
.
这将导致脚本在第一个错误时停止,但通过提交,它将使您能够看到您取得的进展(假设插入顺序中有一些逻辑顺序)

这是一种快速而肮脏的方法来磨练错误

第二种方法是,插入提示以显示插入的值,以帮助调试:

WHENEVER SQLERROR EXIT FAILURE ROLLBACK
PROMPT INSERTING a, b, c
INSERT INTO t1 VALUES ( 'a', 'b', 'c');
PROMPT INSERTING a1, b1, c1
INSERT INTO t1 VALUES ( 'a1', 'b1', 'c1');
.
.
另一个想法,稍微复杂一点。假设您使用EXCEL生成INSERT语句,您可以将它们作为对pr_INSERT过程的调用生成,然后在过程中使用逻辑捕获并标记错误:

CREATE OR REPLACE PROCEDURE pr_insert
   ( p1 IN t1.c1%TYPE,
     p2 IN t1.c2%TYPE,
     p3 IN t1.c3%TYPE ) AS
BEGIN
   INSERT INTO t1 VALUES ( p1, p2, p3 );
EXCEPTION
   WHEN OTHERS THEN
      RAISE_APPLICATION_ERROR ( -20001, 'Error on : ' || p1 || ',' || p2 || 'p3 );
END ;
/
可以在Excel中生成以下pr_插入调用:

BEGIN
  pr_insert ( 'a', 'b', 'c' );
  pr_insert ( 'a1', 'b1', 'c1' );
  pr_insert ( 'a2', 'b2', 'c2' );
.
.
END;
/

当插入失败时,您没有收到错误吗?将sqlplus输出到一个文件中,并对文件中的错误进行grep。非常感谢@TenG,它非常有用,而且是完美的答案。结尾部分有点混乱,如果可能的话,你能解释一下吗?是的,我正在Excel中准备插入脚本。重新设置“结束部分”,创建存储过程pr_insert以接受插入值作为参数,并在插入失败时生成打印值的异常。然后在Excel中,生成“pr_INSERT(…)”语句,而不是生成“INSERT…”语句。