1.
I notice in your trigger, your code says:
INSERT INTO LYNX.TRANS_AUDIT
(TA_ID, TA_KEY_VALUE, TA_TABLE_CHANGED, TA_FIELD_CHANGED, ...)
VALUES
(TA_ID, N.UNIT_ID , 'PUNIT', 'FLEET_ID', ...);
But shouldn't the VALUES clause contain vTA_ID
instead?
INSERT INTO LYNX.TRANS_AUDIT
(TA_ID, TA_KEY_VALUE, TA_TABLE_CHANGED, TA_FIELD_CHANGED, ...)
VALUES
(vTA_ID, N.UNIT_ID , 'PUNIT', 'FLEET_ID', ...);
However, calculating vTA_ID with your query, may not be your most efficient option.
2.
You could create a SEQUENCE object, and let it always calculate new values.
CREATE SEQUENCE TRANS_AUDIT_SEQ AS INTEGER;
Then in your trigger,
INSERT INTO LYNX.TRANS_AUDIT
(TA_ID, TA_KEY_VALUE, TA_TABLE_CHANGED, TA_FIELD_CHANGED, ...)
VALUES
(NEXT VALUE FOR TRANS_AUDIT_SEQ, N.UNIT_ID , 'PUNIT', 'FLEET_ID', ...);
3.
But what may suit you best, may be to define your ID column AS IDENTITY
CREATE TABLE LYNX.TRANS_AUDIT (
TA_ID INTEGER GENERATED BY DEFAULT AS IDENTITY,
...
PRIMARY KEY (TA_ID)
);
Then in your trigger, you do not need to specify the value, since it will be generated for you:
INSERT INTO LYNX.TRANS_AUDIT
(TA_KEY_VALUE, TA_TABLE_CHANGED, TA_FIELD_CHANGED, ...)
VALUES
(N.UNIT_ID , 'PUNIT', 'FLEET_ID', ...);
This is most likely the best of the 3 solutions for you, unless there are other factors not mentioned.
Best Answer
Your UPDATE statement is wrong. It is updating all the records in the table because it doesn't have any WHERE clause. If you want to update only the current row, you need to use the updated/inserted records (like in Jack's example: REFERENCING NEW AS N_ROW and use it as filter).