Oracle数据库!!!行级触发器!!!:new:old怎么用????谢谢!! 20

 我来答
射手小小王
2015-06-02 · TA获得超过398个赞
知道小有建树答主
回答量:716
采纳率:85%
帮助的人:258万
展开全部
CREATE OR REPLACE TRIGGER IUD_BBBB
BEFORE insert or update or delete on EM_AAAA
for each row

declare
-- local variables here
v_count number;
begin

If Inserting Then

select count(*)
into v_count
from em_outlive
where fact_no = :new.fact_no
and pnl_no = :new.pnl_no
and ((nvl(:new.if_flag,'0') = '1' and nvl(in_date,'0') = '0') or
(nvl(:new.if_flag,'0') = '1' and nvl(in_date,'0') <> '0' and in_date >= :new.date_start) or
(nvl(:new.if_flag,'0') = '0' and nvl(in_date,'0') = '0' and out_date <= :new.date_end ) or
(nvl(:new.if_flag,'0') = '0' and nvl(in_date,'0') <> '0' and in_date >= :new.date_start and out_date <= :new.date_end));

if v_count > 0 then
RAISE_APPLICATION_ERROR(-20001,'此员工在日期:'||:new.date_start||'之后已经登记外宿,不可以在厂里登记住宿!');
ROLLBACK;
RETURN ;
end if;

if nvl(:new.if_flag,'0') = '1' then
update em_bed
set use = '1'
where fact_no = :new.fact_no
and build_no = :new.build_no
and floor_no = :new.floor_no
and room_no = :new.room_no
and bed_no = :new.bed_no;

elsif nvl(:new.if_flag,'0') = '0' then
update em_bed
set use = '0'
where fact_no = :new.fact_no
and build_no = :new.build_no
and floor_no = :new.floor_no
and room_no = :new.room_no
and bed_no = :new.bed_no;

end if;

end if;

if Updating then

select count(*)
into v_count
from em_outlive
where fact_no = :new.fact_no
and pnl_no = :new.pnl_no
and ((nvl(:new.if_flag,'0') = '1' and nvl(in_date,'0') = '0') or
(nvl(:new.if_flag,'0') = '1' and nvl(in_date,'0') <> '0' and in_date >= :new.date_start) or
(nvl(:new.if_flag,'0') = '0' and nvl(in_date,'0') = '0' and out_date <= :new.date_end ) or
(nvl(:new.if_flag,'0') = '0' and nvl(in_date,'0') <> '0' and in_date >= :new.date_start and out_date <= :new.date_end));

if v_count > 0 then
RAISE_APPLICATION_ERROR(-20001,'此员工在日期:'||:new.date_start||'之后已经登记外宿,不可以在厂里登记住宿!');
ROLLBACK;
RETURN ;
end if;

if nvl(:new.if_flag,'0') = '1' and nvl(:old.if_flag,'0') = '0' then
update em_bed
set use = '1'
where fact_no = :new.fact_no
and build_no = :new.build_no
and floor_no = :new.floor_no
and room_no = :new.room_no
and bed_no = :new.bed_no;

elsif nvl(:new.if_flag,'0') = '0' and nvl(:old.if_flag,'0') = '1'then
update em_bed
set use = '0'
where fact_no = :new.fact_no
and build_no = :new.build_no
and floor_no = :new.floor_no
and room_no = :new.room_no
and bed_no = :new.bed_no;
end if;
end if;

if Deleting then
if nvl(:old.if_flag,'0') = '1' then
update em_bed
set use = '0'
where fact_no = :old.fact_no
and build_no = :old.build_no
and floor_no = :old.floor_no
and room_no = :old.room_no
and bed_no = :old.bed_no;

end if;
end if;

end IUD_EM_BEDPNL;
scholar165
2015-06-02 · TA获得超过487个赞
知道小有建树答主
回答量:882
采纳率:50%
帮助的人:232万
展开全部
:new跟:old都是一条记录,对于插入炒作前者有效,对于删除后者有效,对于更新两者都有效
追问
能举个例子么??谢谢!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式