create or replace trigger AUTOLHH before insert on bd_cubasdoc for each row declare -- local variables here vcombineaccnum char(15); vpk_custbank char(20); begin select accaddr,pk_custbank into vcombineaccnum,vpk_custbank from bd_custbank where pk_cubasdoc=:new.pk_cubasdoc; insert into bd_accbank (unitname,combineaccnum) values (:new.custname,vcombineaccnum ) where pk_accbank=vpk_custbank end AUTOLHH;
更新6.18 17:58
下面的触发器居然可行:
create or replace trigger AUTONAME2 before insert or update on bd_custbank for each row declare -- local variables here vname varchar2(80); begin select custname into vname from bd_cubasdoc where pk_cubasdoc=:new.pk_cubasdoc; update bd_accbank set combineaccnum=:new.accaddr,unitname=vname where pk_accbank=:new.pk_accbank; end AUTONAME2;
19:01更新:
测试证明,不能update on 只能insert on
另外,更改联行号只能在点击开户银行之后,如果直接在增行的bd_custbank更改,此时联行号不会向bd_custbank回写,保存之后系统仍是旧的,没有更新。
另外,如果直接在增行的bd_custbank更改账号,开户银行也会更改,但是触发器update的东西就会清空
另外,直接在增行的bd_custbank更改银行明细系统是不可更改,灰色的。
create or replace trigger AUTONAME2 before insert on bd_custbank for each row declare -- local variables here vname varchar2(80); begin select custname into vname from bd_cubasdoc where pk_cubasdoc=:new.pk_cubasdoc; update bd_accbank set combineaccnum=:new.accaddr,unitname=vname where pk_accbank=:new.pk_accbank; end AUTONAME2;
22:48更新:
在bd_accbank表上还有另外一个触发器,检测联行号或者开户银行或者单位名称空格的:
create or replace trigger CSpcace_ACCBANK before insert or update on bd_accbank for each row declare -- local variables here begin if regexp_like(:new.bankacc,'[[:space:]]' ) or regexp_like(:new.combineaccnum, '[[:space:]]') or regexp_like(:new.unitname, '[[:space:]]') then raise_application_error(- 20001,'银行账号或联行号或单位名称有空格,请修改!' ); end if ; end CSpcace_ACCBANK;
因为我这个触发器也会触发的时候插入到bd_accbank的单位名称或者联行号字段,我试着在录入客商的时候把客商名称带有空格,发现不会有
raise_application_error(- 20001,'银行账号或联行号或单位名称有空格,请修改!' )的提示框出来(因为触发器不是on在bd_accbank上),症状只是点击保存保存不了。
下面是itpub的帖子:
截图中从上到下是三个表,分包是客商表bd_cubasdoc,客商账户表bd_custbank,银行账户表bd_accbank。
我们目前增加客商,分三个过程,分别对应上面三个表。
1,在第一个表录入客商名称等基础信息;
2,点击增行,录入客商的银行账号,银行名称,之后保存。
3,保存之后,点击第一个表上的开户银行按钮,弹出银行账户信息,这个表的银行账号,银行名称在步骤2自动带过来,只需要补录我红色方框的联行号和单位名称。
三个表的关系:
bd_cubasdoc.pk_cubasdoc=bd_custbank.pk_cubasdoc
bd_custbank.pk_accbank=bd_accbank.pk_accbank。
我现在想做一个触发器,省略上述的过程3。
我修改了第二个表,增加了一个联行号输入框(即下面触发器中的accaddr),这样在步骤2点击保存之后,将这个联行号插入到步骤3的bd_accbank表中的联行号,将步骤1录入的客商名称插入到bd_accbank表中的单位名称。
我简单写的触发器思路如下:
create or replace trigger AUTOLHH
before insert on bd_cubasdoc
for each row
declare
-- local variables here
vcombineaccnum char(15);
vpk_custbank char(20);
begin
select accaddr,pk_custbank into vcombineaccnum,vpk_custbank from bd_custbank
where pk_cubasdoc=:new.pk_cubasdoc;
insert into bd_accbank
(unitname,combineaccnum)
values
(:new.custname,vcombineaccnum )
where pk_accbank=vpk_custbank
end AUTOLHH;
这个触发器我没有完成,问题出现在insert的时候,因为在操作步骤1和2的时候系统已经有了对第三个表的insert,我现在不知道怎么写(因为我要插入的就两个字段:联行号和单位名称),是否应该写after触发器,然后update第三个表呢?
更新:21:35,我整理了一下思路,这样写了触发器:
create or replace trigger AUTONAME
after insert on bd_cubasdoc
for each row
declare
-- local variables here
vcombineaccnum char(30);
vpk_accbank char(30);
begin
select accaddr,pk_accbank into vcombineaccnum,vpk_accbank from bd_custbank
where pk_cubasdoc=:new.pk_cubasdoc; --根据表1录入客商信息的:new.pk_cubasdoc取联行号和表3的pk_accbank
update bd_accbank set unitname=:new.custname,combineaccnum=vcombineaccnum
where pk_accbank=vpk_accbank;--使用after insert on触发器更新表3
end AUTONAME;
触发器编译没有问题,可是在软件端录入了数据保存的时候,保存不了,是触发器的问题,请教。
原因的debug截图,真的找不到数据啊
ava:129) at nc.bs.framework.comn.serv.CommonServletDispatcher.doGet(CommonServletDispatcher.java:76) at nc.bs.framework.comn.serv.CommonServletDispatcher.doPost(CommonServletDispatcher.java:95) [AWT-EventQueue-2] ERROR - ORA-01403: 未找到数据 ORA-06512: 在 "XMV502.AUTONAME", line 7 ORA-04088: 触发器 'XMV502.AUTONAME' 执行过程中出错 java.lang.RuntimeException: ORA-01403: 未找到数据 ORA-06512: 在 "XMV502.AUTONAME", line 7 ORA-04088: 触发器 'XMV502.AUTONAME' 执行过程中出错 at nc.impl.uap.bd.cust.CustBasDocDAO.insert(CustBasDocDAO.java:783) at nc.impl.uap.bd.cust.CubasdocImpl.insert(CubasdocImpl.java:75) at nc.impl.uap.bd.BusiScmEJB.insert(BusiScmEJB.java:590) at nc.itf.uap.bd.BusiScmEJBEjbBean.insert(BusiScmEJBEjbBean.java:344) at nc.itf.uap.bd.BusiScmEJB_Local.insert(BusiScmEJB_Local.java:230
更新:22:45游标itpub的写法:
复杂的要死,准备问下如果不用cursor写法而强制使用cursor写法会造成的后果?
create or replace trigger AUTONAME after insert on bd_cubasdoc for each row declare -- local variables here vcombineaccnum varchar2(80); vpk_accbank char(20); cursor basjob(id char) is select accaddr,pk_accbank from bd_custbank where pk_cubasdoc = id and nvl(dr, 0) = 0; begin open basjob(:new.pk_cubasdoc); loop fetch basjob into vcombineaccnum ,vpk_accbank ; exit when basjob%notfound; end loop; close basjob; --raise_application_error(-20001,:new.custname||:new.pk_cubasdoc); if vcombineaccnum is not null and vpk_accbank is not null then raise_application_error(-20001,:new.custname||:new.pk_cubasdoc); begin --raise_application_error(-20001,:new.custname||:new.pk_cubasdoc); update bd_accbank set unitname=:new.custname,combineaccnum=vcombineaccnum where pk_accbank=vpk_accbank; end; end if; end AUTONAME;