我必须创建将继续审核我的表的触发器。触发器应该在插入和更新时执行。
目前我有两个触发器
一个用于插入:
CREATE TRIGGER SCH.TRG_TBL1_AFT_I
AFTER INSERT ON SCH.TBL1
REFERENCING
NEW AS n
FOR EACH ROW
MODE DB2SQL
INSERT INTO SCH.TBL1_AUDIT
VALUES( .. ,, .. );
另一个更新
CREATE TRIGGER SCH.TRG_TBL1_AFT_U
AFTER UPDATE ON SCH.TBL1
REFERENCING
NEW AS n
FOR EACH ROW
MODE DB2SQL
INSERT INTO SCH.TBL1_AUDIT
VALUES( .. ,, .. );
但问题是,如果可以在DB2中创建单个触发器来执行任务吗? [提供两者,触发器正在做同样的事情。]
答案 0 :(得分:3)
试试这个
CREATE or replace TRIGGER PASSENGER_TR01_BEFORE_IUD
BEFORE
DELETE
OR UPDATE OF FIRST_NAME
OR INSERT ON PASSENGER
REFERENCING
OLD AS oldRow
NEW AS newRow
FOR EACH ROW
WHEN (1=1)
Begin
Declare ACTION Char(1) Default '';
-- Use Case/When to inquire trigger-event
Case
When INSERTING Then
Set ACTION='I';
When UPDATING Then
Set ACTION='U';
When DELETING Then
Set ACTION='D';
Else
Set ACTION='N';
End Case;
-- Use If/Then/Else to inquire trigger-event
If INSERTING Then
Set ACTION='I';
ElseIf UPDATING Then
Set ACTION='U';
ElseIf DELETING Then
Set ACTION='D';
Else
Set ACTION='N';
End If;
End
答案 1 :(得分:1)
是的,这是可能的。的 See the documentation of create trigger 即可。这是一个粘贴:
trigger-event
.-OR--------------------------------------.
V (4) |
|----+-INSERT--------------------------+-----+------------------|
+-DELETE--------------------------+
'-UPDATE--+---------------------+-'
| .-,-----------. |
| V | |
'-OF----column-name-+-'
这会让你说:
create trigger blah before insert on blah or update of blah
。
答案 2 :(得分:0)
此功能现在位于DB2 10.x版本中。 它已经可用正弦9.7.x
答案 3 :(得分:0)
CREATE OR REPLACE TRIGGER SET_SALARY
NO CASCADE
BEFORE UPDATE OR INSERT ON employee
REFERENCING NEW AS n OLD AS o
FOR EACH ROW
WHEN( o.edlevel IS NULL OR n.edlevel > o.edlevel )
BEGIN
-- Give 10% raise to existing employees with new education level.
IF UPDATING THEN SET n.salary = n.salary * 1.1;
-- Give starting salary based on education level.
ELSEIF INSERTING THEN
SET n.salary = CASE n.edlevel WHEN 18 THEN 50000
WHEN 16 THEN 40000
ELSE 25000
END;
END IF;
END
答案 4 :(得分:-1)
很抱歉,DB2没有提供将更新和插入触发器组合在一起的方法。