Oracle dblink应用

查看dblink

select owner,object_name from dba_objects where object_type='DATABASE LINK';
or
select * from dba_db_links;

创建dblink

前提:
创建dblink的用户有对应的数据库权限

create public database link 或者create database link ;
grant create public database link,create database link to myAccount;
create public database link dblinkname connect to username identified by password
   using '(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = database_ip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME =servicename)
)
)';

如果在create之后不加public,则创建的dblink就不是公共的,就只有创建者可以使用了.

  • 如何确定数据库的servicename:
  1. sqlplus中使用
show parameter[s] service_names;

注意parameter和parameters都可以
或者

select name,value from v$parameter where name='service_names';

使用db link

例如,在本机数据库上创建了一个scott_rmthost的public dblink(使用远程主机的scott用户连接),则用sqlplus连接到本机数据库,执行select * from scott.emp@scott_rmthot即可以将远程数据库上的scott用户下的emp表中的数据获取到.
也可以在本地建一个同义词来指向scott.emp@scott_rmthost,这样取值就方便多了.

删除dblink

drop public database link dblinkname;

例:

create public database link chengwenit connect to chengweniterp
identified by "123456"    using '(DESCRIPTION = (ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.102)(PORT = 1521)) )
(CONNECT_DATA = (SERVICE_NAME =CWIT) ) )';

Oracle循环语句参考示例

--备份错误数据
create table bms_batch_def_noprice0714 as(
select *
  from bms_batch_def t
 where nvl(t.unitprice, 0) = 0
   and t.createfrom = 7);
--修改错误数据
begin
  for c1 in (select *
   from bms_batch_def t
  where nvl(t.unitprice, 0) = 0
    and t.createfrom = 7) loop
    update bms_batch_def a
 set (a.notaxsuprice, a.unitprice) =
     (select b.notaxsuprice, b.unitprice
  from bms_batch_def b
 where b.batchid = c1.oldbatchid)
     where a.batchid = c1.batchid;
  end loop;
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农文哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值