ORACLE

>建立dblink
create database link link_sync_user --连接名称,自定义
connect to itouch_fda_ent identified by itouch123  --用户名和密码(如果报密码错加上引号试试)
using '(DESCRIPTION = 
(ADDRESS_LIST = 
(ADDRESS = (PROTOCOL = TCP)(HOST = 118.122.124.78)(PORT = 9521)) --数据库IP地址和端口号
) 
(CONNECT_DATA = 
(SERVICE_NAME = orcl) --数据库实例名
) 
)'

>同一服务器

create database link link_branch     --输入所要创建dblink的名称,自定义 
connect to sg_syj identified by sg_syj  --设置连接远程数据库的用户名和密码
using '192.168.196.76/branch';  --指定目标数据库的连接方式,可用tns名称,即数据库实例名(ip可省略)

>测试dblink

select * from ENT_BASIC@link_sync_user
where ent_id = '40288583544b833701544c7e8b9b0507'

>删除dblink

drop database link link_sync_user

>创建触发器
create or replace trigger a_b_test
  after insert or update or delete 
  on SG_QY_INFO
  for each row --针对表中某行
begin
  if deleting then
      delete from ENT_BASIC@link_sync_user where ENT_ID=:old.QY_ID;
-- 	end if;
-- 	if inserting then  //将if/end if格式改为ELSIF格式
  ELSIF inserting then
      insert into ENT_BASIC@link_sync_user(ENT_ID,ENT_CODE)
      values(:new.QY_ID,:new.AREA_ID); 
  ELSIF updating then
     update ENT_BASIC@link_sync_user set ENT_CODE=:new.AREA_ID where ENT_ID=:old.QY_ID;
  end if;end a_b_test;
--end a_b_test; //将这句放在end if同一行,紧跟其后

>如果报ORA-24344:成功,但出现编译错误. 按上面注释修改

>也可通过merge完成同步

create or replace trigger trigger_sync_user
after insert or update
  on UC_USER --针对整个表
begin
	merge into SYS_ADMIN@link_sync_user u using UC_USER cas on (u.USERNAME = cas.LOGIN_NAME)
	when matched then
		update set u.REALNAME = cas.USER_NAME,u.AREAID = SUBSTR(cas.ORG_ID, 1, 6)
	when not matched then
		insert(u.ID,u.USERNAME,u.USERPWD,u.AREAID,u.OPERATIONDATE,u.REALNAME) 
		values(cas.USER_ID,cas.LOGIN_NAME,cas.LOGIN_PWD,SUBSTR(cas.ORG_ID, 1, 6),cas.OPER_CTIME,cas.USER_NAME);
end;
merge




你可能感兴趣的:(oracle,触发器,数据库,DBlink,数据同步)