CREATE trigger ttbpedido_item_a01u for tbpedido_item
active after update position 1
AS
DECLARE VARIABLE CONFERENCIA_ITEM_ID INTEGER;
BEGIN
    
  IF(OLD.QTD_EST <> NEW.QTD_EST) THEN
  BEGIN
        SELECT FIRST 1
            K.ID
        FROM TBCONFERENCIA_ITEM K, TBCONFERENCIA C
        WHERE K.CONFERENCIA_ID = C.ID
        AND C.TABELA = 'PED'
        AND C.TABELA_ID = NEW.PEDIDO
        AND K.TABELA_ITEM_ID = NEW.CONTROLE INTO :CONFERENCIA_ITEM_ID;
    
        IF(COALESCE(:CONFERENCIA_ITEM_ID, 0) > 0) THEN
        BEGIN

           UPDATE TBCONFERENCIA_ITEM T SET
                T.QTD_ALOCADA       = NEW.QTD_EST,
                T.QTD_DISPONIVEL    = NEW.QTD_EST
           WHERE T.ID = :CONFERENCIA_ITEM_ID;

        END

  END
END;