在删除oracle触发器之前检查值,然后插入另一个表中

时间:2011-09-09 10:40:22

标签: oracle triggers

目前我们在tbl_number上有一个触发器。 如下,

create or replace TRIGGER TRIGGER_EXAMPLE
BEFORE DELETE
ON TBL_NUMBER
FOR EACH ROW
BEGIN


-- Insert record into TBL_NUMBER _DUMMY table
INSERT INTO TBL_NUMBER _DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, :old.NO_OF_PEOPLE)
 END;

现在我们要修改触发器 并在原始表tbl_number中检查值NO_OF_People 如果是(-1),我们想在表tbl_number_dummy的NO_of_People列中放入0(或null)。

任何有关它的指示都将不胜感激。

谢谢, -Adi

1 个答案:

答案 0 :(得分:2)

INSERT INTO TBL_NUMBER_DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, decode(:old.NO_OF_PEOPLE, -1, 0, :old.NO_OF_PEOPLE)
 END;

或者,如果您想仅在NO_OF_PEOPLE = -1时插入TBL_NUMBER _DUMMY,

CASE WHEN :old.NO_OF_PEOPLE = -1 THEN
INSERT INTO TBL_NUMBER _DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, :old.NO_OF_PEOPLE)
ELSE null;
END;

更新(对评论的回应):

INSERT INTO TBL_NUMBER_DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, case when :old.NO_OF_PEOPLE < 0 then 0; else :old.NO_OF_PEOPLE; end)
 END;

或者,带解码

INSERT INTO TBL_NUMBER_DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, decode(sign(:old.NO_OF_PEOPLE), -1, 0, :old.NO_OF_PEOPLE)
 END;

:)

是的,你可以在数字列中插入null,除了列有NOT NULL的情况。约束