我正在嘗試編寫觸發器來控制記錄是否已在表中。如果記錄已在表中(例如按名稱比較),那麼當前記錄集合valid ='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;
我不會那樣做觸發器。 –
表A上的觸發器通常無法查詢表A,因爲該表的數據當前處於通量狀態。而且觸發器也不能在另一個表中插入/更新。您正在編寫一個觸發器,假定該行被插入,在正確的情況下,應該再次插入該行。有解決方法,但是對於你所問的問題,我會建議編寫一個你的應用程序調用的PL/SQL過程來檢查,然後根據需要進行插入或更新。 –
它不起作用? –