建DBLINK:
使用pl/sql developer建:找到Database Links,右键新建
名称:dblink名 连接到用户名:目标数据库登录名 密码:目标数据库密码
数据库:目标数据库服务名
查询表:
select * from 用户名.表 @DBLINK名称 where 条件;
查询函数:
select 用户名.函数名@DBLINK名称(参数) from dual;
在本地函数中调用dblink函数:
Result:=用户名.函数名@DBLINK名称(参数);
复制dblink中的表结构与数据:
CREATE TABLE 表名 AS SELECT * FROM 用户名.表名@DBLINK名称 where 条件
索引这些可以使用手工建:在pl/sql developer的SQL窗口中选中表名再查看表结构
备注:
如果机子上同时安装ORACLE的数据库与客户端,要用数据库建需连接dblink的数据库的服务
在过程中创建表时要先给权限execUTE immediate 'Grant Create any table to 用户名';
从dblink的从张表中取数只需在每个表名后加@dblink名称
如何通过动态sql远程调用包里面的函数,并返回值。
我简单做了一个例子,实现以上要求.
首先进行适当授权:
[oracle@jumper oracle]$ sqlplus "/ as sysdba"SQL*Plus: Release 9.2.0.4.0 - Production on Tue Nov 7 21:07:56 2006
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning option
JServer Release 9.2.0.4.0 - ProductionSQL> grant create public database link to eygle;
Grant succeeded.
SQL> grant all on dbms_flashback to eygle;Grant succeeded.
建立DB Link:
SQL> connect eygle/eygle
Connected.
SQL> create public database link hsbill using 'hsbill';Database link created.
SQL> select db_link from dba_db_links;
DB_LINK
---------------------------------------------------
HSBILLSQL> select * from dual@hsbill;
D
-
X
此后可以尝试使用DB Link进行远程和本地执行:
SQL> set serveroutput on
SQL> set feedback off
SQL> declare
2 r_gname varchar2(40);
3 l_gname varchar2(40);
4 begin
5 execute immediate
6 'select GLOBAL_NAME from global_name@hsbill' into r_gname;
7 dbms_output.put_line('gname of remote:'||r_gname);
8 select GLOBAL_NAME into l_gname from global_name;
9 dbms_output.put_line('gname of locald:'||l_gname);
10 end;
11 /
gname of remote:HSBILL.HURRAY.COM.CN
gname of locald:EYGLE
远程Package或Function调用也可以随之实现:
SQL> declare
2 r_scn number;
3 l_scn number;
4 begin
5 execute immediate
6 'select dbms_flashback.GET_SYSTEM_CHANGE_NUMBER@hsbill from dual' into r_scn;
7 dbms_output.put_line('scn of remote:'||r_scn);
8 end;
9 /
scn of remote:18992092687
SQL>
-The End-