如何从 SQL *PLUS 中的批量插入中查找失败的插入语句
How to find failed insert statements from bulk inserts in SQL *PLUS
我一直在尝试通过 SQL *PLUS 在我的 table 中插入近 10K 行。但是,当我计算 SQL 时,DEVELOPER 在 10K 行中丢失了近 10 行。
如何查找插入失败的行table?
我已经在 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');
.
.
这将导致脚本在第一个错误处停止,但是使用 COMMIT 它将使您能够看到您得到了多远(假设插入顺序中有一些逻辑顺序)。
这是一种找出错误的快速而肮脏的方法。
第二种方法是散布 PROMPT 以显示正在插入哪些值以协助调试:
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 ;
/
以下pr_insert个调用可以在您Excel中产生:
BEGIN
pr_insert ( 'a', 'b', 'c' );
pr_insert ( 'a1', 'b1', 'c1' );
pr_insert ( 'a2', 'b2', 'c2' );
.
.
END;
/
我一直在尝试通过 SQL *PLUS 在我的 table 中插入近 10K 行。但是,当我计算 SQL 时,DEVELOPER 在 10K 行中丢失了近 10 行。
如何查找插入失败的行table?
我已经在 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');
.
.
这将导致脚本在第一个错误处停止,但是使用 COMMIT 它将使您能够看到您得到了多远(假设插入顺序中有一些逻辑顺序)。
这是一种找出错误的快速而肮脏的方法。
第二种方法是散布 PROMPT 以显示正在插入哪些值以协助调试:
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 ;
/
以下pr_insert个调用可以在您Excel中产生:
BEGIN
pr_insert ( 'a', 'b', 'c' );
pr_insert ( 'a1', 'b1', 'c1' );
pr_insert ( 'a2', 'b2', 'c2' );
.
.
END;
/