日期:2014-05-17  浏览次数:20788 次

oracle 触发器的更新两张数据表
我想通过插入一张表的信息时候更新另一张表,该怎么弄呢,求教,如下数据:
drop table t_info;
create table t_info(
infoid number(3) primary key,
infoname char(20),
static char(20));

drop table t_lent;
create table t_lent(
lentid number(3) primary key,
infoid number(3),
lentname char(20) not null
);

insert into t_info values(1,'洗衣机','可用');
insert into t_info values(2,'电冰箱','可用');
create trigger lenttoinfo
after insert on t_lent for
each row
begin
update t_info
set static:='已使用' where infoid = old.infoid;
end;
insert into t_lent values(1,1,'洗衣服');
请问为什么我的触发器不正确呢,怎么写才正确呢。
我想要的结果插t_lent表时候,t_info表标记已使用。

------解决方案--------------------
没弄懂你的意思。是要在插入t_lent表的时候同时更新t_info表吗?如果是,代码如下
create trigger lenttoinfo
after insert on t_lent for
each row
begin
update t_info
set static='已使用' where infoid = old.infoid;
end;
------解决方案--------------------
create trigger lenttoinfo
after insert on t_lent for
each row
declare
begin
update t_info
set static='已使用' where infoid = :new.infoid;
end;
------解决方案--------------------
实测修改编译通过:
SQL code

CREATE TRIGGER lenttoinfo
AFTER INSERT ON t_lent FOR EACH ROW
BEGIN
    UPDATE t_info SET STATIC ='已使用' WHERE infoid = :old.infoid;
END;