在Oracle的存储过程中使用dblink调用远程存储过程备忘

 

使用dblink的方式有两种,我使用的第二种,也是比较方便的一种,因为不用修改tnsnames.ora文件

 

写了一个调用远端sayHello存储过程的存储过程,远端sayHello存储过程如下:

 

-- 远端 sayHello 定义如下: create or replace procedure sayHello(currentTime out varchar) is begin select 'Hello World' into currentTime from dual; end sayHello;

实现的sayRemoteHello存储过程如下:

create or replace procedure sayRemoteHello(name varchar, pwd varchar, ip varchar) authid current_user as sqlstr varchar(1000); r varchar(100); begin sqlstr:='drop database link tlink'; execute immediate sqlstr; -- sqlstr:= 'create database link tlink connect to scott identified by fj1234 using ''(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.70)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = orcl)))'''; -- SERVICE_NAME 使用 SELECT * FROM GLOBAL_NAME; 可查,默认为orcl sqlstr:= 'create database link tlink connect to '||name||' identified by '||pwd||' using ''(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = '||ip|| ')(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = orcl)))'''; execute immediate sqlstr; sqlstr:='begin sayHello@tlink(:a); end;'; execute immediate sqlStr using out r; dbms_output.put_line(r); end sayRemoteHello; 


测试如下:

--测试:call sayRemoteHello('scott', 'fj1234', '192.168.0.70'); 

你可能感兴趣的:(备忘)