DELIMITER $$

CREATE TRIGGER delete_awbimport
    AFTER UPDATE
    ON awbexport FOR EACH ROW
BEGIN
    -- statements
	
	
	SELECT 
	ID, IID, export_ok 
FROM 
	v_awbstat_congruity_stati
WHERE 
	    IID IS NOT NULL 
	AND ID = IID 
	AND export_ok = 0
	AND ID = ??
LIMIT 1
	
	

	DECLARE val INTEGER;
	SELECT 
		CAST(decvalue AS DECIMAL(10,5)) AS decvalue
	INTO val
    FROM `decode`
    WHERE 
		    (`decode`.`decclass` = 'CONGRUITA') 
		and (`decode`.`deckey` = CONCAT(deckeyval, '.inf.100'))
	LIMIT 1;
		
    IF val100 IS NULL THEN
	SET retVal = NULL;
        SET iRetVal = NULL;		
    ELSE
	SET retVal = valueIn * (val100 / 100000.0);
        SET iRetVal = CAST(retVal * 1000.0 AS SIGNED);
    END IF;
    IF valNum IS NOT NULL AND (iRetVal IS NULL OR iRetVal > valNum) THEN
	SET retVal = CAST(valNum AS DECIMAL(10,5)) / 1000.0;
    END IF;	
    IF (retVal IS NULL) THEN
        RETURN NULL;
    ELSE
	RETURN CAST((valueIn - retVal) * 1000.0 AS SIGNED);
    END IF;
	
	
	
	
	
	
END$$    

DELIMITER ;