oracle 触发器只执行insert不执行update,delete了
createorreplacetriggertri_UserafterinsertorupdateordeleteonUSER_MAPPINGreferencingold...
create or replace trigger tri_User
after insert or update or delete
on USER_MAPPING
referencing old as old_value
new as new_value
for each row
declare UNIQUEID varchar(32);
IDCARD varchar(18);
PHONENUM varchar(11);
c_num integer;
begin
SELECT COUNT(*) INTO c_num FROM USERS WHERE UNIQUEID = :new_value.UNIQUEID;
IF c_num = 1 THEN /* 1 存在 更新 删除 */
-- update UNIQUEID
--dbms_output.put_line(1);
if updating then
update USERS set IDCARD = :new_value.IDCARD,PHONENUM=:new_value.PHONENUM
where UNIQUEID = :new_value.UNIQUEID;
end if;
-- delete
if deleting then
delete from USERS where UNIQUEID = :old_value.UNIQUEID;
end if;
ELSIF c_num < 1 THEN -- 0 不存在 就插入 1 不执行
--insert 唯一ID (UNIQUEID ) 身份证号(IDCARD)手机号(PHONENUM) *\
-- insert UFNIQUEID
-- dbms_output.put_line(2);
if :new_value.IDCARD is null THEN
IDCARD := '';
end if;
if :new_value.PHONENUM is null THEN
PHONENUM :='';
end if;
if inserting then
insert into USERS(UNIQUEID,IDCARD,PHONENUM)
values(:new_value.UNIQUEID, :new_value.IDCARD, :new_value.PHONENUM);
end if;
END IF;
end;
user_mapping 是主表 users 是子表 ,像user_mapping中插入数据同时插入到users中,如果第二次插入时候判断如果users中有uniqueid就更新或者删除,不执行insert 展开
after insert or update or delete
on USER_MAPPING
referencing old as old_value
new as new_value
for each row
declare UNIQUEID varchar(32);
IDCARD varchar(18);
PHONENUM varchar(11);
c_num integer;
begin
SELECT COUNT(*) INTO c_num FROM USERS WHERE UNIQUEID = :new_value.UNIQUEID;
IF c_num = 1 THEN /* 1 存在 更新 删除 */
-- update UNIQUEID
--dbms_output.put_line(1);
if updating then
update USERS set IDCARD = :new_value.IDCARD,PHONENUM=:new_value.PHONENUM
where UNIQUEID = :new_value.UNIQUEID;
end if;
-- delete
if deleting then
delete from USERS where UNIQUEID = :old_value.UNIQUEID;
end if;
ELSIF c_num < 1 THEN -- 0 不存在 就插入 1 不执行
--insert 唯一ID (UNIQUEID ) 身份证号(IDCARD)手机号(PHONENUM) *\
-- insert UFNIQUEID
-- dbms_output.put_line(2);
if :new_value.IDCARD is null THEN
IDCARD := '';
end if;
if :new_value.PHONENUM is null THEN
PHONENUM :='';
end if;
if inserting then
insert into USERS(UNIQUEID,IDCARD,PHONENUM)
values(:new_value.UNIQUEID, :new_value.IDCARD, :new_value.PHONENUM);
end if;
END IF;
end;
user_mapping 是主表 users 是子表 ,像user_mapping中插入数据同时插入到users中,如果第二次插入时候判断如果users中有uniqueid就更新或者删除,不执行insert 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询