我在同一个库里进了两个用户test1和test2,并在这两个用户下建了同样结构的表table1和table2,
id varchar2(8) not null,name varchar2(8),constraint pk_table2 primary key(id)
在test1下,使用以下语句创建到test2的table2的同义词和用于测试的存储过程
create database link link_test2 connect to test2 identified by test2 using 'test';
create synonym table2 for table2@link_test2;create or replace procedure proc1 is
begin
insert into table1(id, name)
select id, name from table2;
commit;
exception
when others then
rollback;
proc2;
end proc1;create or replace procedure proc2 is
cursor table2_cursor is select * from table2;
count_loop number default 0;
begin
for table2_row in table2_cursor
loop
count_loop := count_loop + 1;
end loop;
exception
when others then
rollback;
RAISE_APPLICATION_ERROR(-20256, TO_CHAR(SQLCODE) || SQLERRM);
end proc2;在test2的table2下插入一条数据,运行proc1更新数据到test1的table1,再次运行proc1
因为主键冲突调用proc2,当游标数据取完再到for table2_row in table2_cursor时跳到
异常报-1ORA-00001违反唯一性约束条件(.),但不用database link,而用
create synonym table2 for test2.table2则相同的流程不报此错误,想知道相关原因,
越详细越好!谢谢!
id varchar2(8) not null,name varchar2(8),constraint pk_table2 primary key(id)
在test1下,使用以下语句创建到test2的table2的同义词和用于测试的存储过程
create database link link_test2 connect to test2 identified by test2 using 'test';
create synonym table2 for table2@link_test2;create or replace procedure proc1 is
begin
insert into table1(id, name)
select id, name from table2;
commit;
exception
when others then
rollback;
proc2;
end proc1;create or replace procedure proc2 is
cursor table2_cursor is select * from table2;
count_loop number default 0;
begin
for table2_row in table2_cursor
loop
count_loop := count_loop + 1;
end loop;
exception
when others then
rollback;
RAISE_APPLICATION_ERROR(-20256, TO_CHAR(SQLCODE) || SQLERRM);
end proc2;在test2的table2下插入一条数据,运行proc1更新数据到test1的table1,再次运行proc1
因为主键冲突调用proc2,当游标数据取完再到for table2_row in table2_cursor时跳到
异常报-1ORA-00001违反唯一性约束条件(.),但不用database link,而用
create synonym table2 for test2.table2则相同的流程不报此错误,想知道相关原因,
越详细越好!谢谢!
补充说明:只有在
insert into table1(id, name) select id, name from table2;
异常时调用proc2才报错,若直接用proc2;替换以上的insert则也不报错!
在test1下,第一次运行proc1时因为test1的table1里还没有那条数据,
所以proc1的insert将test2的table2的数据更新过来。
因为proc1的insert不执行异常不调用proc2所以不报错。
没说要更新table2啊,原本要实现的功能是从table2拷数据到table1,
先尝试insert(如果table1中无table2的相同主键的数据可以提高效率),
如果尝试失败再调用proc2用游标去去数据进行update或insert,
现在把proc2简化了下便于测试,这些都不是问题的关键,我要问的是为什么用了database link后去
insert尝试失败,再调用proc2时游标那个循环执行到最后会跳到异常部分去。