有乙個表,表名是xx,有abcd四個字段,正常情況下,abc、abd這三個欄位都可以唯一確定一條記錄,按理應該做成唯一索引,但由於歷史原因,該錶存在重複資料,但要刪掉哪一條需要人工判斷,無法用語句批量刪除,於是唯一索引加不上。但為了保證以後資料的準確性,需要控制新插進去的記錄是唯一的。於是我寫了乙個觸發器,當新插進去的記錄與現有記錄重複時就報錯:
create global temporary table g_xx_temp
(flatdocid int,
modeldocid int,flatdocrev int,modeldocrev int) on commit delete rows;
create or replace trigger trg_xx_row
before insert or update
on xx
for each row
declare
-- local variables here
begin
insert into g_xx_temp(flatdocid,modeldocid,flatdocrev,modeldocrev)
values(:new.flatdocid,:new.modeldocid,:new.flatdocrev,:new.modeldocrev);
end;
create or replace trigger trg_xx
after insert or update
on xx
--for each row
declare
-- local variables here
v_count int;
v_flatdocid int;
v_modeldocid int;
v_flatdocrev int;
v_modeldocrev int;
cursor c_temp is select flatdocid,modeldocid,flatdocrev,modeldocrev from g_xx_temp;
begin
Oracle觸發器的乙個簡單例子
使用者狀態變成離職狀態,系統配置表被觸發,修改配置表狀態 create or replace trigger basedb userleft before update or delete on basedb users for each row declare local variables he...
乙個觸發器的例子
create or replace trigger usremhr trg sq rule insert before insert on sq rule base for each row declare temp rule id varchar2 10 temp rul theme id var...
Oracle觸發器例子
create table emp his as select from emp where1 2 create orreplace trigger tr del emp before delete 指定觸發時機為刪除操作前觸發 onscott.emp for each row 說明建立的是行級觸發器...