赞
踩
1.首先建立dblink:
- -- 创建dblink(testlink为dblink的名称,xt是用户名,第二个xt是密码,using后是ip+端口+实例)
- create public database link testlink connect to xt identified by "xt" USING '127.0.0.1:1521/orcl”'
2.建立触发器:
- -- 创建user_info的触发器(user_trigger是触发器的名称)
-
- create or replace trigger user_trigger
- after insert or update or delete -- 意思是在做完添加、更新、删除后触发
- on xt.USER_INFO -- 在user_info 表中操作完之后触发
- for each row
- begin
- if deleting then -- @testlink意思是操作另一方库的user_info表
- delete from xt.USER_INFO@testlink where userid=:old.userid;
- end if;
- if inserting then
- insert into xt.USER_INFO@testlink(userid,username,userno,login,pw,sfzh,sex,birthday,nativeplace,nation,zjmm,zwlx,headship1,zzdz,bgdh,mobile,fax,email,res,photoid,telephone,xsxh,userscbz,headship2,position1,position2)
- values(:new.userid,:new.username,:new.userno,:new.login,:new.pw,:new.sfzh,:new.sex,:new.birthday,:new.nativeplace,:new.nation,:new.zjmm,:new.zwlx,:new.headship1,:new.zzdz,:new.bgdh,:new.mobile,:new.fax,:new.email,:new.res,:new.photoid,:new.telephone,:new.xsxh,:new.userscbz,:new.headship2,:new.position1,:new.position2);
- end if;
- if updating then
- update xt.USER_INFO@TESTLINK set userid = :new.userid ,username=:new.username,userno=:new.userno,login=:new.login,pw=:new.pw,sfzh=:new.sfzh,sex=:new.sex,birthday=:new.birthday,nativeplace=:new.nativeplace,nation=:new.nation,zjmm=:new.zjmm,zwlx=:new.zwlx,headship1=:new.headship1,zzdz=:new.zzdz,bgdh=:new.bgdh,mobile=:new.mobile,fax=:new.fax,email=:new.email,res=:new.res,photoid=:new.photoid,telephone=:new.telephone,xsxh=:new.xsxh,userscbz=:new.userscbz,headship2=:new.headship2,position1=:new.position1,position2=:new.position2 where userid=:old.userid;
- end if;
- end user_trigger;
3.注意事项:
防止触发器产生多余数据,在做更新操作是,务必将userid = :new.userid 写到set后面,包括添加操作;否则会产生重复数据
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。