我试图在 DB2 数据库中创建一个触发器,该触发器在更新一个表中的列时运行,然后用某些值填充另一个表。
例如,有一个带有一FLEET_ID
列的功率单位表。每次FLEET_ID
更改时,我都需要在TRANS_AUDIT
表中创建一个新行。
TRANS_AUDIT
架构如下:
CREATE TABLE LYNX.TRANS_AUDIT (
TA_ID INTEGER NOT NULL,
TA_KEY_VALUE VARCHAR(100),
TA_TABLE_CHANGED VARCHAR(40),
TA_FIELD_CHANGED VARCHAR(40),
TA_OLD_FIELD_VALUE VARCHAR(100),
TA_NEW_FIELD_VALUE VARCHAR(100),
TA_USER_WHO_CHANGED VARCHAR(128),
TA_DATE_CHANGED TIMESTAMP,
TA_COMMENT VARCHAR(40),
TA_OLD_FIELD_DOUBLE DOUBLE DEFAULT 0,
TA_NEW_FIELD_DOUBLE DOUBLE DEFAULT 0,
PRIMARY KEY (TA_ID)
);
这是我到目前为止所拥有的,但我似乎无法让它工作,我遇到了一个function sequence
错误。
CREATE TRIGGER PU_UPD_FLEETID
AFTER UPDATE OF FLEET_ID ON PUNIT
REFERENCING OLD AS O NEW AS N
FOR EACH ROW
MODE DB2SQL
BEGIN ATOMIC
DECLARE
vTA_ID INTEGER;
IF(N.FLEET_ID <> O.FLEET_ID) THEN
SELECT MAX(TA_ID)+1 FROM TRANS_AUDIT INTO vTA_ID; --generate a unique sequential id
INSERT INTO LYNX.TRANS_AUDIT (TA_ID, TA_KEY_VALUE, TA_TABLE_CHANGED, TA_FIELD_CHANGED, TA_OLD_FIELD_VALUE, TA_NEW_FIELD_VALUE, TA_USER_WHO_CHANGED, TA_DATE_CHANGED, TA_COMMENT, TA_OLD_FIELD_DOUBLE, TA_NEW_FIELD_DOUBLE)
VALUES (TA_ID, N.UNIT_ID , 'PUNIT', 'FLEET_ID', O.FLEET_ID, N.FLEET_ID , SESSION_USER ,CURRENT TIMESTAMP , '', '0' ,'0' );
END IF;
END;