我正在尝试编写触发器,它将控制记录是否已在表中。如果记录已经在表中(例如按名称比较),那么当前记录设置有效='False'并插入新的。有什么办法吗?
这是我的想法,但它不起作用。
create or replace TRIGGER
Check_r
before insert on t$customer
FOR each ROW
declare
v_dup number;
v_com number;
v_id number;
v_id_new number;
begin
v_date:=SYSDATE;
select count(id) INTO v_dup from t$customer where surname=:NEW.surname ;
select count(id) INTO v_com from t$customer where firstname =:NEW.firstname and
address=:NEW.address;
select id into v_id from t$customer where surname=:NEW.surname;
if v_dup > 0 and v_com=0 then
v_id_new:= m$_GET_ID; -- get id
update t$customer set valid = 'False' where id = v_id;
insert into t$customer ( id, surname ,firstname, valid, address ) values (v_id_new,:NEW.surname ,:NEW.firstname, :NEW.valid, :NEW.address);
end if;
if v_dup = 0 then
v_id_new:= m$_GET_ID; -- get id
insert into t$customer ( id, surname ,firstname, valid , address) values (v_id_new,:NEW.surname ,:NEW.firstname, :NEW.valid, :NEW.address);
end if;
end;
答案 0 :(得分:0)
首先,这是插入的触发器。你不需要写插入语句。
其次,您需要更新旧记录。只需使用where子句更新它。
CREATE OR REPLACE TRIGGER Check_r
before insert on t$customer
FOR each ROW
BEGIN
UPDATE t$customer set valid = 'False'
WHERE surname = :NEW.surname
AND firstname =:NEW.firstname;
:NEW.id := m$_GET_ID;
END;
答案 1 :(得分:0)
您可以使用复合触发器,例如:
{{j-check checked=isOnline action="refreshModel" }}
请注意,这个解决方案很丑陋且性能不佳! 但它应该给你一个如何运作的印象。
通常,您应将所有这些放入PL / SQL过程而不是触发器。