Subject Confusing behavior of Before Insert/Update trigger
Author Marcin Bury
Hello all

I have following trigger defined in my database:

CREATE OR ALTER TRIGGER PINEDA_IHS_WLR_ITEMS_BIU FOR PINEDA_IHS_WLR_ITEMS
ACTIVE BEFORE INSERT OR UPDATE POSITION 0
AS
DECLARE VARIABLE KURS_ID INTEGER;
DECLARE VARIABLE ORDER_NR VARCHAR(50);
BEGIN
IF (INSERTING)
THEN BEGIN
IF ((NEW.IHS_WLR_ITEM_ID IS NULL) OR (NEW.IHS_WLR_ITEM_ID = 0))
THEN NEW.IHS_WLR_ITEM_ID = GEN_ID(PINEDA_GEN_IHS_WLR_ITEMS,1);
IF ((NEW.WLR_VER IS NULL) OR (NEW.WLR_VER = 0) )
THEN NEW.WLR_VER = 1;
END

IF ((INSERTING) OR (NEW.WLR_VER IS DISTINCT FROM OLD.WLR_VER))
THEN BEGIN
SELECT KURS_ID FROM PINEDA_IHS_WLR
WHERE IHS_WLR_ID = NEW.IHS_WLR_ID
INTO :KURS_ID;
ORDER_NR = NEW.ORDER_NUMBER || '-' || NEW.ORDER_LINE_ITEM_NUMBER;
SELECT ID, SRV
FROM MGP_ROLE R
WHERE KOD_UIB = NEW.UIB_CODE AND ORDER_NR = :ORDER_NR
AND R.SRV IN (SELECT SRV FROM SP_TRANS TR WHERE TR.NR_KURSU
= :KURS_ID)
INTO NEW.ROLA_ID, NEW.SRV;
IF ((INSERTING) AND (NEW.ROLA_ID IS NULL))
THEN NEW.WLR_VER = 0;
END

END

Confusing thing is when the trigger is executed 'BEFORE INSERT', select
from MGP_ROLE table returns two nulls, but when the same trigger is
executed 'BEFORE UPDATE' due to change of WLR_VER column value, select
from MGP_ROLE returns expected values.
I made following test - I inserted some records to IHS_WLR_ITEMS table
via my application - all of them had nulls in ROLA_ID and SRV columns.
Then I updated WLR_VER column and trigger filled ROLA_ID and SRV columns
with proper values.
I can assure that all values required by select from MGP_ROLE table are
provided when inserting records and the update statement run to execute
trigger code only changes value of WLR_VER column.

Can it be a bug or am I missing something?

TIA
Marcin