referencing OLD as old NEW as new --这里是什么意思?这实际上是默认的写法,去掉也一样的。 你还可以写成referencing OLD as oldvalue new as newvalue oldvalue表示被操作的记录原来的值,newvalue表示被操作记录的新值。使用时用:newvalue.字段,:oldvalue.字段。:应该是表示结合变量的意思
:new是新值的缺省别名, :old是更新之前的旧值。
referencing OLD as old NEW as new 指定缺省的OLD和NEW分别为old和new,避免触发器作用的表名称刚好为OLD或NEW而发生冲突。
分开来看的话:插入时的触发: if INSERTING then select nvl(max(substr(heat_no,3)),0) into v_heat_counter from st_heat where heat_no like to_char(sysdate, 'YY')||'%';v_heat_counter := v_heat_counter + 1; v_pon := 'P'||substr(to_char(sysdate,'YY'),2,1)||lpad(to_char(v_heat_counter),4,'0'); v_prod_date:=trunc(sysdate); --上面只是给变量v_pon,v_prod_date赋值对吗? insert into st_heat (pon,heat_no,mo,steel_code,route,prod_date,liq_steel_weight) values(v_pon,:new.heat_no,:new.mo,:new.steel_code,:new.route,v_prod_date,:new.liq_steel_weight); --那么我上面使用 :new.heat_no或者 ,:old.heat_no是不是都一样?因为这个值并没有变化。 --是不是也可以直接用 heat_no呢?
多谢几位,那么应该是与本触发器中的update触发还有关系了 部分代码: elsif UPDATING('MO') OR UPDATING('HEAT_NO') then update st_heat set mo=:new.mo,heat_no=:new.heat_no where mo=:old.mo and heat_no=:old.heat_no; 其中的 UPDATING('MO')是不是指,如果erp_moi_10表记录中MO,heat_no有更新(别的列更新不管),则 会update st_heat表啊?
整理了一下,不過我想還是要知道具體的不能插入是什麼情況下的:)CREATE OR REPLACE TRIGGER trg_erp_moi_10 after insert or update or delete on erp_moi_10 referencing OLD as old NEW as new for each row declare v_heat_counter number; v_pon varchar2(6); v_prod_date date; begin if INSERTING then select nvl(max(substr(heat_no,3)),0) into v_heat_counter from st_heat where heat_no like to_char(sysdate, 'YY')||'%'; v_heat_counter := v_heat_counter + 1; v_pon := 'P'||substr(to_char(sysdate,'YY'),2,1)||lpad(to_char(v_heat_counter),4,'0'); v_prod_date:=trunc(sysdate); insert into st_heat ( pon , heat_no , mo , steel_code , route , prod_date , liq_steel_weight ) values(v_pon , :new.heat_no , :new.mo , :new.steel_code , :new.route , v_prod_date , :new.liq_steel_weight); elsif UPDATING('MO') OR UPDATING('HEAT_NO') then
update st_heat set mo=:new.mo , heat_no=:new.heat_no where mo=:old.mo and heat_no=:old.heat_no; elsif DELETING THEN DELETE FROM ST_HEAT WHERE MO=:old.mo and heat_no=:old.heat_no; end if;end;
你还可以写成referencing OLD as oldvalue new as newvalue
oldvalue表示被操作的记录原来的值,newvalue表示被操作记录的新值。使用时用:newvalue.字段,:oldvalue.字段。:应该是表示结合变量的意思
:old是更新之前的旧值。
答: :new为新值的代名词, :new.heat_no为新的值
如下:
insert into tbl ( ...., heat_no)values (...., 4)
则在trigger中 :new.heat_no = 4update tbl set heat_no = 5
则在trigger中 :new.heat_no = 5
:old.heat_no = 4
if INSERTING then
select nvl(max(substr(heat_no,3)),0) into v_heat_counter from st_heat
where heat_no like to_char(sysdate, 'YY')||'%';v_heat_counter := v_heat_counter + 1;
v_pon := 'P'||substr(to_char(sysdate,'YY'),2,1)||lpad(to_char(v_heat_counter),4,'0');
v_prod_date:=trunc(sysdate);
--上面只是给变量v_pon,v_prod_date赋值对吗?
insert into st_heat (pon,heat_no,mo,steel_code,route,prod_date,liq_steel_weight)
values(v_pon,:new.heat_no,:new.mo,:new.steel_code,:new.route,v_prod_date,:new.liq_steel_weight);
--那么我上面使用 :new.heat_no或者 ,:old.heat_no是不是都一样?因为这个值并没有变化。
--是不是也可以直接用 heat_no呢?
部分代码:
elsif UPDATING('MO') OR UPDATING('HEAT_NO') then
update st_heat set mo=:new.mo,heat_no=:new.heat_no where mo=:old.mo and heat_no=:old.heat_no;
其中的
UPDATING('MO')是不是指,如果erp_moi_10表记录中MO,heat_no有更新(别的列更新不管),则
会update st_heat表啊?
《入门提高必读》上册
http://www.cnoug.org/viewthread.php?tid=21207
《入门提高必读》下册
http://www.cnoug.org/viewthread.php?tid=21650
所以比较着急,我会去看书。从没接触过oracle,呵呵
会update st_heat表啊?是的
after insert or update or delete on erp_moi_10
referencing OLD as old NEW as new
for each row
declare
v_heat_counter number;
v_pon varchar2(6);
v_prod_date date;
begin
if INSERTING then
select nvl(max(substr(heat_no,3)),0)
into v_heat_counter
from st_heat
where heat_no like to_char(sysdate, 'YY')||'%'; v_heat_counter := v_heat_counter + 1;
v_pon := 'P'||substr(to_char(sysdate,'YY'),2,1)||lpad(to_char(v_heat_counter),4,'0');
v_prod_date:=trunc(sysdate); insert into st_heat (
pon ,
heat_no ,
mo ,
steel_code ,
route ,
prod_date ,
liq_steel_weight )
values(v_pon ,
:new.heat_no ,
:new.mo ,
:new.steel_code ,
:new.route ,
v_prod_date ,
:new.liq_steel_weight); elsif UPDATING('MO') OR UPDATING('HEAT_NO') then
update st_heat
set mo=:new.mo ,
heat_no=:new.heat_no
where mo=:old.mo
and heat_no=:old.heat_no; elsif DELETING THEN DELETE FROM ST_HEAT
WHERE MO=:old.mo
and heat_no=:old.heat_no; end if;end;
我要做的是,在.net程序里象表中插入数据而已。
但是由于担心病毒,所以只给我们分了一个网口,我们这里只有一个机器可以访问oracle服务器。
开始时,我们这里可以用sql plus插入数据(一条),但是在程序里没有报错,只是一点响应都没有。
无奈之下以为网络太慢,昨天就搬主机到oracle所在的网络测试,程序也可以插入数据(执行sql语句2秒钟),但是第一次可以,第2次再插入非相同MO(主键)的记录时就会报错,具体错误没记主。
大概意思就是说trg_erp_moi_10的事,说是已经存在此MO的记录(说的是唯一性的事),但是明白着我的这个表中没有这个MO的。
不过看这个出发器影响的表是st_heat. 会不会是st_heat也有一个插入的触发呢?
update st_heat
set mo=:new.mo ,
heat_no=:new.heat_no
where mo=:old.mo
and heat_no=:old.heat_no;