/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Author:  dsv01
 * Created: 23/06/2016
 */

CREATE TRIGGER D004_Up_Peso AFTER UPDATE ON T066 FOR EACH ROW BEGIN
    DECLARE xPesoAtual DECIMAL(10,4);
    
    IF OLD.T066_D004_Id!=0 THEN
        SELECT 
                sum(T066_Peso_Atual)
            INTO 
                xPesoAtual
            FROM 
                T066
            WHERE 
                T066_D004_Id=OLD.T066_D004_Id;

        UPDATE 
                D004
            SET 
                D004_Peso_Atual = xPeso_Atual
            WHERE 
                D004_Id = OLD.T066_D004_Id;
    END IF;
END;

