今天开发给发邮件过来说一个存储过程想通过dblink往目标库insert数据,但报错ORA-02069: global_names parameter must be set to TRUE for this operation,想让我根据错误提示在数据库上修改global_names参数修改为True。
先来看看官方文档是如何介绍这个参数的:
GLOBAL_NAMES specifies whether a database link is required to have the same name as the database to which it connects.
If the value of GLOBAL_NAMES is false, then no check is performed. If you use or plan to use distributed processing, then Oracle recommends that you set this parameter to true to ensure the use of consistent naming conventions for databases and links in a networked environment.
从下面的查询可以看出global_names参数可以在线进行修改的
[email protected]>col name for a30 [email protected]>select name,ISSES_MODIFIABLE,ISSYS_MODIFIABLE from v$parameter where name=‘global_names‘; NAME ISSES_MODIFIABL ISSYS_MODIFIABLE ------------------------------ --------------- --------------------------- global_names TRUE IMMEDIATE
看完了这个参数,再来看看开发的存储过程代码,其中insert语句中是用到了一个序列,所以导致了这个报错。先在测试数据库上创建了一个简单的存储过程来模拟现再这个问题
创建一个dblink
[email protected]>create database link link_orcl connect to zx identified by "zx" using ‘orcl‘; Database link created. [email protected]>select * from [email protected]_orcl; DUM --- X
先创建一个不带序列的远程insert的存储过程
[email protected]>create or replace procedure pro_a as 2 begin 3 insert into [email protected]_orcl (c1) values(‘a‘); 4 commit; 5 end; 6 / Procedure created.
执行这个存储过程,观察结果,数据可以正常插入
[email protected]>select * from [email protected]_orcl; no rows selected [email protected]>exec pro_a; PL/SQL procedure successfully completed. [email protected]>select c1 from [email protected]_orcl; C1 --- a
创建一个序列,并修改上面的存储过程
[email protected]>create sequence seq_a; Sequence created. [email protected]>create or replace procedure pro_a as 2 begin 3 insert into [email protected]_orcl (c1,n1) values(‘a‘,seq_a.nextval); 4 commit; 5 end; 6 / Procedure created.
执行修改后的存储过程,重现上面的错误ORA-02069
[email protected]>exec pro_a; BEGIN pro_a; END; * ERROR at line 1: ORA-02069: global_names parameter must be set to TRUE for this operation ORA-06512: at "ZX.PRO_A", line 3 ORA-06512: at line 1
先在session层面修改global_names参数,再次执行存储过程,又出现了新的错误:说两端的数据库名不一致。
[email protected]>alter session set global_names = true; Session altered. [email protected]>exec pro_a; BEGIN pro_a; END; * ERROR at line 1: ORA-02085: database link LINK_ORCL connects to ORCL ORA-06512: at "ZX.PRO_A", line 3 ORA-06512: at line 1 [email protected]>!oerr ora 2085 02085, 00000, "database link %s connects to %s" // *Cause: a database link connected to a database with a different name. // The connection is rejected. // *Action: create a database link with the same name as the database it // connects to, or set global_names=false.
那现在问题来了,实际生产中源端和目标端的数据库名肯定是不一致的,所以修改这个参数并不能解决这个问题。
只能想其他的办法来绕过这个错误,这里给开发提了两个建议:
1、把存储过程部署到目标端来避免远程insert中调用sequence
2、在源端存储过程中引入临时表,先把数据插入临时表,再从临时表插入到远端表。
在MOS上搜到了一个相关文档(ORA-02069 DURING REMOTE INSERT OF A LOCAL SEQUENCE (文档 ID 1047673.6))跟我们的问题描述一致。
官方文档:http://docs.oracle.com/cd/E11882_01/server.112/e40402/initparams098.htm#REFRN10065