oracle中更新触发器怎么写,求指点,谢谢
在ORACLE中我想写个触犯器.两个表table1(dangan,bingan,nianxian),Table2(dangan,bingan,fenlei,zhi).当t...
在ORACLE中我想写个触犯器.两个表table1(dangan,bingan,nianxian),Table2(dangan,bingan,fenlei,zhi).当table2中满足fenlei='0000',如果Table2中的zhi有变化,那么Table1中的nianxian,就等于Table2中的zhi的数据.这个触发器怎么写,求指点
展开
2个回答
展开全部
你好,触发器如下,经过测试哦!、
创建触发器:
CREATE OR REPLACE TRIGGER TR_UPDATE
AFTER UPDATE ON TABLE2---当修改了TABLE2时触发
FOR EACH ROW
when (OLD.FENLEI='0000')--当tabel2的fenlei值为0000时触发操作
BEGIN
CASE
WHEN UPDATING('ZHI') THEN
UPDATE TABLE1 SET nianxian=:NEW.ZHI WHERE dangan=:OLD.dangan AND bingan=:OLD.bingan;
END CASE;
END;
修改tabel2的数据,即可在table1中看到结果,希望能采取我的答案哦~
创建触发器:
CREATE OR REPLACE TRIGGER TR_UPDATE
AFTER UPDATE ON TABLE2---当修改了TABLE2时触发
FOR EACH ROW
when (OLD.FENLEI='0000')--当tabel2的fenlei值为0000时触发操作
BEGIN
CASE
WHEN UPDATING('ZHI') THEN
UPDATE TABLE1 SET nianxian=:NEW.ZHI WHERE dangan=:OLD.dangan AND bingan=:OLD.bingan;
END CASE;
END;
修改tabel2的数据,即可在table1中看到结果,希望能采取我的答案哦~
更多追问追答
追问
会提示 COMMIT不能在触发器中
追答
哦,我重新修改了,commit去掉,刚刚粘贴的时候贴错了,呵呵
展开全部
参考一下:
CREATE OR REPLACE TRIGGER "TRI_TABLE2"
AFTER INSERT OR UPDATE OR DELETE ON TABLE2
FOR EACH ROW
BEGIN
IF UPDATING THEN
update table1 t1 set t1.nianxian = (select t2.zhi from table t2
where 1=1
and t1.dangan = t2.dangan
and t1.bingan = t2.bingan
and t2.fenlei = '0000'
)
where 1=1
and exists (select 1 from table t2
where t1.dangan = t2.dangan
and t1.bingan = t2.bingan
and t2.fenlei = '0000')
;
END IF;
END TRI_TABLE1;
/
CREATE OR REPLACE TRIGGER "TRI_TABLE2"
AFTER INSERT OR UPDATE OR DELETE ON TABLE2
FOR EACH ROW
BEGIN
IF UPDATING THEN
update table1 t1 set t1.nianxian = (select t2.zhi from table t2
where 1=1
and t1.dangan = t2.dangan
and t1.bingan = t2.bingan
and t2.fenlei = '0000'
)
where 1=1
and exists (select 1 from table t2
where t1.dangan = t2.dangan
and t1.bingan = t2.bingan
and t2.fenlei = '0000')
;
END IF;
END TRI_TABLE1;
/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询