CREATE OR REPLACE PROCEDURE NUTRIENTE_DO_CARDAPIO_PME
  (P_CDFILCENTPME IN VARCHAR2,
   P_CDFILIAL     IN VARCHAR2,
   P_CDSERVFILI   IN VARCHAR2,
   P_NUTRIENTES   IN VARCHAR2,
   P_NRSEQUNUTR   IN VARCHAR2,
   P_IDNDPCAL     IN VARCHAR2,
   P_DTINICIAL    IN DATE,
   P_DTFINAL      IN DATE,
   P_CDTIPOCARD   IN VARCHAR2) IS
   V_QTCARBOIDRATO NUMBER;
   V_QTPROTEINA    NUMBER;
   V_QTLIPIDEO     NUMBER;
   V_QTALCOOL      NUMBER;
   V_QTCALORIA     NUMBER;
BEGIN
   FOR C_NUTRTRAB IN (
      SELECT CD.CDFILCENTPME, CD.CDFILIAL, CD.CDSERVFILI, CD.DTCARDIAPME, 'XXXX' AS CDDIETPAD,
             CC.NRSEQPRACPME, CC.CDTIPOPRAT, CC.CDFILAUXPRAT, CC.CDPRATO, RS.CDPRODUTO,
             RS.SGUNIDADE AS SGUNIDPROD, NP.CDNUTRIENTE, NVL(RS.QTCALCNUTRSE,0) AS QTPRODUTO,
             NVL((NP.QTNUTRPROD * 10 * NVL(DECODE(PRODFILI.VRPESOUNIDFI, 0, PR.VRPESOUNID, PRODFILI.VRPESOUNIDFI), PR.VRPESOUNID) * RS.QTCALCNUTRSE), 0) AS QTNUTPROD,
             NVL(CC.VRPECONSCPME,0) AS VRPECONSCARD, RS.QTBRUTASEUNR AS QTPRODPRATT
        FROM PRODFILI, PRODUTO PR, NUTRPROD NP, COMPCARDPME CC, RECESERV RS, CARDAPIOPME CP,
             (SELECT P.CDFILCENTPME, P.NRCARDPME, P.NRCARDIAPME, P_CDFILIAL AS CDFILIAL, P.CDSERVFILI, P.DTCARDIAPME
                FROM CARDIAPME P, CARDAPIOPME
               WHERE (P.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,P.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (P.CDFILIAL     = P_CDFILCENTPME)  
                 AND (P.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND NOT EXISTS(SELECT CDFILIAL
                                  FROM  CARDIAPME C
                                 WHERE (C.CDFILCENTPME = P.CDFILCENTPME) 
                                   AND (C.NRCARDPME    = P.NRCARDPME )    
                                   AND (C.CDSERVFILI   = P.CDSERVFILI)   
                                   AND (C.CDFILIAL     = P_CDFILIAL  )     
                                   AND (C.DTCARDIAPME  = P.DTCARDIAPME)  
                                   AND (C.IDCARDESPEC  = 'S'))
                AND CARDAPIOPME.CDFILCENTPME = P.CDFILCENTPME
                AND CARDAPIOPME.NRCARDPME = P.NRCARDPME
                AND P.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME
               UNION ALL
              SELECT D.CDFILCENTPME, D.NRCARDPME, D.NRCARDIAPME, P_CDFILIAL AS CDFILIAL, D.CDSERVFILI, D.DTCARDIAPME
                FROM CARDIAPME D, CARDAPIOPME
               WHERE (D.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,D.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (D.CDFILIAL     = P_CDFILIAL)     
                 AND (D.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND (D.IDCARDESPEC  = 'S')
                 AND CARDAPIOPME.CDFILCENTPME = D.CDFILCENTPME
                 AND CARDAPIOPME.NRCARDPME = D.NRCARDPME
                 AND D.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME) CD
       WHERE (CD.CDFILCENTPME    = CC.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CC.NRCARDPME)        
         AND (CD.NRCARDIAPME     = CC.NRCARDIAPME)      
         AND (CD.CDFILIAL        = RS.CDFILIAL)         
         AND (CD.CDSERVFILI      = RS.CDSERVFILI)       
         AND (CC.CDFILAUXPRAT    = RS.CDFILAUXPRAT)     
         AND (CC.CDPRATO         = RS.CDPRATO)          
         AND (PRODFILI.CDFILIAL  = CD.CDFILCENTPME)     
         AND (PRODFILI.CDPRODUTO = PR.CDPRODUTO)
         AND (P_NUTRIENTES = 'T' OR (INSTR(P_NUTRIENTES,NP.CDNUTRIENTE) <> 0)) 
         AND (RS.CDPRODUTO = NP.CDPRODUTO)              
         AND (RS.CDPRODUTO = PR.CDPRODUTO)              
         AND (CD.CDFILCENTPME    = CP.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CP.NRCARDPME)        
         AND (CP.CDTIPOCARD      = P_CDTIPOCARD)
       UNION ALL
      SELECT CD.CDFILCENTPME, CD.CDFILIAL, CD.CDSERVFILI, CD.DTCARDIAPME, 'XXXX' AS CDDIETPAD,
             CC.NRSEQPRACPME, CC.CDTIPOPRAT, CC.CDFILAUXPRAT, CC.CDPRATO, RF.CDPRODUTO,
             RF.SGUNIDADE AS SGUNIDPROD, NP.CDNUTRIENTE, NVL(RF.QTCALCNUTRLO,0) AS QTPRODUTO,
             NVL((NP.QTNUTRPROD * 10 * NVL(DECODE(PRODFILI.VRPESOUNIDFI, 0, PR.VRPESOUNID, PRODFILI.VRPESOUNIDFI), PR.VRPESOUNID) * RF.QTCALCNUTRLO), 0) AS QTNUTPROD,
             NVL(CC.VRPECONSCPME,0) AS VRPECONSCARD, RF.QTBRUTALOUNR AS QTPRODPRATT
        FROM PRODFILI, PRODUTO PR, NUTRPROD NP, COMPCARDPME CC, RECEFILI RF, CARDAPIOPME CP,
             (SELECT P.CDFILCENTPME, P.NRCARDPME, P.NRCARDIAPME, P_CDFILIAL AS CDFILIAL, P.CDSERVFILI, P.DTCARDIAPME
                FROM CARDIAPME P, CARDAPIOPME
               WHERE (P.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,P.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (P.CDFILIAL     = P_CDFILCENTPME)  
                 AND (P.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND NOT EXISTS(SELECT CDFILIAL
                                  FROM  CARDIAPME C
                                 WHERE (C.CDFILCENTPME = P.CDFILCENTPME) 
                                   AND (C.NRCARDPME    = P.NRCARDPME)    
                                   AND (C.CDSERVFILI   = P.CDSERVFILI)   
                                   AND (C.CDFILIAL     = P_CDFILIAL)     
                                   AND (C.DTCARDIAPME  = P.DTCARDIAPME)  
                                   AND (C.IDCARDESPEC  = 'S'))
                AND CARDAPIOPME.CDFILCENTPME = P.CDFILCENTPME
                AND CARDAPIOPME.NRCARDPME = P.NRCARDPME
                AND P.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME
               UNION ALL
              SELECT D.CDFILCENTPME, D.NRCARDPME, D.NRCARDIAPME, P_CDFILIAL AS CDFILIAL, D.CDSERVFILI, D.DTCARDIAPME
                FROM CARDIAPME D, CARDAPIOPME
               WHERE (D.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,D.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (D.CDFILIAL     = P_CDFILIAL)     
                 AND (D.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND (D.IDCARDESPEC  = 'S')
                 AND CARDAPIOPME.CDFILCENTPME = D.CDFILCENTPME
                 AND CARDAPIOPME.NRCARDPME = D.NRCARDPME
                 AND D.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME) CD
       WHERE (CD.CDFILCENTPME    = CC.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CC.NRCARDPME)        
         AND (CD.NRCARDIAPME     = CC.NRCARDIAPME)      
         AND (CD.CDFILIAL        = RF.CDFILIAL)         
         AND (CC.CDFILAUXPRAT    = RF.CDFILAUXPRAT)     
         AND (CC.CDPRATO         = RF.CDPRATO)          
         AND (PRODFILI.CDFILIAL  = CD.CDFILCENTPME)     
         AND (PRODFILI.CDPRODUTO = PR.CDPRODUTO)
         AND (P_NUTRIENTES = 'T' OR (INSTR(P_NUTRIENTES,NP.CDNUTRIENTE) <> 0)) 
         AND (RF.CDPRODUTO = NP.CDPRODUTO)              
         AND (RF.CDPRODUTO = PR.CDPRODUTO)              
         AND (CD.CDFILCENTPME    = CP.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CP.NRCARDPME)        
         AND (CP.CDTIPOCARD      = P_CDTIPOCARD)        
         AND NOT EXISTS (SELECT CDPRATO
                           FROM RECESERV
                          WHERE (CDFILAUXPRAT = CC.CDFILAUXPRAT) 
                            AND (CDPRATO      = CC.CDPRATO)      
                            AND (CDFILIAL     = CD.CDFILIAL) 
                            AND (CDSERVFILI   = CD.CDSERVFILI))
       UNION ALL
      SELECT CD.CDFILCENTPME, CD.CDFILIAL, CD.CDSERVFILI, CD.DTCARDIAPME, 'XXXX' AS CDDIETPAD,
             CC.NRSEQPRACPME, CC.CDTIPOPRAT, CC.CDFILAUXPRAT, CC.CDPRATO, RT.CDPRODUTO,
             RT.SGUNIDADE AS SGUNIDPROD, NP.CDNUTRIENTE, NVL(RT.QTCALCNUTRPRTP,0) AS QTPRODUTO,
             NVL((NP.QTNUTRPROD * 10 * NVL(DECODE(PRODFILI.VRPESOUNIDFI, 0, PR.VRPESOUNID, PRODFILI.VRPESOUNIDFI), PR.VRPESOUNID) * RT.QTCALCNUTRPRTP), 0) AS QTNUTPROD,
             NVL(CC.VRPECONSCPME,0) VRPECONSCARD, RT.QTBRUTAPRUNRTP AS QTPRODPRATT
        FROM PRODFILI, PRODUTO PR, NUTRPROD NP, COMPCARDPME CC, RECETPCARD RT, CARDAPIOPME CP,
             (SELECT P.CDFILCENTPME, P.NRCARDPME, P.NRCARDIAPME, P_CDFILIAL AS CDFILIAL, P.CDSERVFILI, P.DTCARDIAPME
                FROM CARDIAPME P, CARDAPIOPME
               WHERE (P.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,P.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (P.CDFILIAL     = P_CDFILCENTPME)  
                 AND (P.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND NOT EXISTS(SELECT CDFILIAL
                                  FROM CARDIAPME C
                                 WHERE (C.CDFILCENTPME = P.CDFILCENTPME) 
                                   AND (C.NRCARDPME    = P.NRCARDPME)    
                                   AND (C.CDSERVFILI   = P.CDSERVFILI)   
                                   AND (C.CDFILIAL     = P_CDFILIAL)     
                                   AND (C.DTCARDIAPME  = P.DTCARDIAPME)  
                                   AND (C.IDCARDESPEC  = 'S'))
                AND CARDAPIOPME.CDFILCENTPME = P.CDFILCENTPME
                AND CARDAPIOPME.NRCARDPME = P.NRCARDPME
                AND P.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME
               UNION ALL
              SELECT D.CDFILCENTPME, D.NRCARDPME, D.NRCARDIAPME, P_CDFILIAL AS CDFILIAL, D.CDSERVFILI, D.DTCARDIAPME
                FROM CARDIAPME D, CARDAPIOPME
               WHERE (D.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,D.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (D.CDFILIAL     = P_CDFILIAL)     
                 AND (D.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND (D.IDCARDESPEC  = 'S')
                 AND CARDAPIOPME.CDFILCENTPME = D.CDFILCENTPME
                 AND CARDAPIOPME.NRCARDPME = D.NRCARDPME
                 AND D.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME) CD
       WHERE (CD.CDFILCENTPME    = CC.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CC.NRCARDPME)        
         AND (CD.NRCARDIAPME     = CC.NRCARDIAPME)      
         AND (RT.CDFILAUXPRAT    = CC.CDFILAUXPRAT)
         AND (CD.CDFILCENTPME    = RT.CDFILCENTPME)
         AND (CC.CDPRATO         = RT.CDPRATO) 
         AND (PRODFILI.CDFILIAL  = CD.CDFILCENTPME)     
         AND (PRODFILI.CDPRODUTO = PR.CDPRODUTO)        
         AND (P_NUTRIENTES = 'T' OR (INSTR(P_NUTRIENTES,NP.CDNUTRIENTE) <> 0)) 
         AND (RT.CDPRODUTO = NP.CDPRODUTO)              
         AND (RT.CDPRODUTO = PR.CDPRODUTO)              
         AND (CD.CDFILCENTPME    = CP.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CP.NRCARDPME)        
         AND (CP.CDTIPOCARD      = P_CDTIPOCARD)        
         AND (RT.CDFILCENTPME    = CP.CDFILCENTPME)
         AND (RT.CDTIPOCARD      = CP.CDTIPOCARD)
         AND NOT EXISTS (SELECT CDPRATO
                           FROM RECEFILI
                          WHERE (CDFILAUXPRAT = CC.CDFILAUXPRAT) 
                            AND (CDPRATO      = CC.CDPRATO)      
                            AND (CDFILIAL     = CD.CDFILIAL)) 
         AND NOT EXISTS (SELECT CDPRATO
                           FROM RECESERV
                          WHERE (CDFILAUXPRAT = CC.CDFILAUXPRAT) 
                            AND (CDPRATO      = CC.CDPRATO)      
                            AND (CDFILIAL     = CD.CDFILIAL) 
                            AND (CDSERVFILI   = CD.CDSERVFILI))
       UNION ALL                  
      SELECT CD.CDFILCENTPME, CD.CDFILIAL, CD.CDSERVFILI, CD.DTCARDIAPME, 'XXXX' AS CDDIETPAD,
             CC.NRSEQPRACPME, CC.CDTIPOPRAT, CC.CDFILAUXPRAT, CC.CDPRATO, RP.CDPRODUTO,
             RP.SGUNIDADE AS SGUNIDPROD, NP.CDNUTRIENTE, NVL(RP.QTCALCNUTRPR,0) AS QTPRODUTO,
             NVL((NP.QTNUTRPROD * 10 * NVL(DECODE(PRODFILI.VRPESOUNIDFI, 0, PR.VRPESOUNID, PRODFILI.VRPESOUNIDFI), PR.VRPESOUNID) * RP.QTCALCNUTRPR), 0) AS QTNUTPROD,
             NVL(CC.VRPECONSCPME,0) VRPECONSCARD, RP.QTBRUTAPRUNR AS QTPRODPRATT
        FROM PRODFILI, PRODUTO PR, NUTRPROD NP, COMPCARDPME CC, RECEPADR RP, CARDAPIOPME CP,
             (SELECT P.CDFILCENTPME, P.NRCARDPME, P.NRCARDIAPME,P_CDFILIAL AS CDFILIAL, P.CDSERVFILI, P.DTCARDIAPME
                FROM CARDIAPME P, CARDAPIOPME
               WHERE (P.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,P.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (P.CDFILIAL     = P_CDFILCENTPME)  
                 AND (P.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND NOT EXISTS(SELECT CDFILIAL
                                  FROM  CARDIAPME C
                                 WHERE (C.CDFILCENTPME = P.CDFILCENTPME) 
                                   AND (C.NRCARDPME    = P.NRCARDPME)    
                                   AND (C.CDSERVFILI   = P.CDSERVFILI)   
                                   AND (C.CDFILIAL     = P_CDFILIAL)     
                                   AND (C.DTCARDIAPME  = P.DTCARDIAPME)  
                                   AND (C.IDCARDESPEC  = 'S'))
                AND CARDAPIOPME.CDFILCENTPME = P.CDFILCENTPME
                AND CARDAPIOPME.NRCARDPME = P.NRCARDPME
                AND P.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME
               UNION ALL
              SELECT D.CDFILCENTPME, D.NRCARDPME, D.NRCARDIAPME, P_CDFILIAL AS CDFILIAL, D.CDSERVFILI, D.DTCARDIAPME
                FROM CARDIAPME D, CARDAPIOPME
               WHERE (D.CDFILCENTPME = P_CDFILCENTPME) 
                 AND ((INSTR(P_CDSERVFILI,D.CDSERVFILI) <> 0) OR (P_CDSERVFILI = 'T')) 
                 AND (D.CDFILIAL     = P_CDFILIAL)     
                 AND (D.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)  
                 AND (D.IDCARDESPEC  = 'S')
                 AND CARDAPIOPME.CDFILCENTPME = D.CDFILCENTPME
                 AND CARDAPIOPME.NRCARDPME = D.NRCARDPME
                 AND D.DTCARDIAPME BETWEEN CARDAPIOPME.DTINICARDPME AND CARDAPIOPME.DTFINCARDPME) CD
       WHERE (CD.CDFILCENTPME    = CC.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CC.NRCARDPME)        
         AND (CD.NRCARDIAPME     = CC.NRCARDIAPME)      
         AND (RP.CDFILAUXPRAT    = 'P')                 
         AND (CC.CDPRATO         = RP.CDPRATO)          
         AND (PRODFILI.CDFILIAL  = CD.CDFILCENTPME)     
         AND (PRODFILI.CDPRODUTO = PR.CDPRODUTO)        
         AND (P_NUTRIENTES = 'T' OR (INSTR(P_NUTRIENTES,NP.CDNUTRIENTE) <> 0)) 
         AND (RP.CDPRODUTO = NP.CDPRODUTO)              
         AND (RP.CDPRODUTO = PR.CDPRODUTO)              
         AND (CD.CDFILCENTPME    = CP.CDFILCENTPME)     
         AND (CD.NRCARDPME       = CP.NRCARDPME)        
         AND (CP.CDTIPOCARD      = P_CDTIPOCARD)        
         AND NOT EXISTS (SELECT CDPRATO
                           FROM RECETPCARD
                          WHERE (CDFILAUXPRAT = CC.CDFILAUXPRAT) 
                            AND (CDPRATO      = CC.CDPRATO)      
                            AND (CDFILCENTPME = CD.CDFILCENTPME)
                            AND (CDTIPOCARD   = CP.CDTIPOCARD))
         AND NOT EXISTS (SELECT CDPRATO
                           FROM RECEFILI
                          WHERE (CDFILAUXPRAT = CC.CDFILAUXPRAT) 
                            AND (CDPRATO      = CC.CDPRATO)      
                            AND (CDFILIAL     = CD.CDFILIAL)) 
         AND NOT EXISTS (SELECT CDPRATO
                           FROM RECESERV
                          WHERE (CDFILAUXPRAT = CC.CDFILAUXPRAT) 
                            AND (CDPRATO      = CC.CDPRATO)      
                            AND (CDFILIAL     = CD.CDFILIAL) 
                            AND (CDSERVFILI   = CD.CDSERVFILI))) LOOP
      INSERT INTO NUTRTRAB
         (NRSEQUNUTR,
          CDFILINUTR,
          CDSERVNUTR,
          DTREFENUTR,
          CDDIETNUTR,
          NRSEQPRATNUT,
          CDTPPRATNUTR,
          CDFAPRATNUTR,
          CDPRATNUTR,
          CDPRODNUTR,
          CDNUTRCALC,
          QTPRODNUTR,
          QTNUTRCALC,
          QTPROTLIQU,
          VRCALORNUTR,
          VRPECONSNUTR,
          SGUNIDPRODT,
          QTPRODPRATT)
      VALUES
         (P_NRSEQUNUTR,
          C_NUTRTRAB.CDFILIAL,
          C_NUTRTRAB.CDSERVFILI,
          C_NUTRTRAB.DTCARDIAPME,
          C_NUTRTRAB.CDDIETPAD,
          C_NUTRTRAB.NRSEQPRACPME,
          C_NUTRTRAB.CDTIPOPRAT,
          C_NUTRTRAB.CDFILAUXPRAT,
          C_NUTRTRAB.CDPRATO,
          C_NUTRTRAB.CDPRODUTO,
          C_NUTRTRAB.CDNUTRIENTE,
          C_NUTRTRAB.QTPRODUTO,
          C_NUTRTRAB.QTNUTPROD,
          0,
          0,
          C_NUTRTRAB.VRPECONSCARD,
          C_NUTRTRAB.SGUNIDPROD,
          C_NUTRTRAB.QTPRODPRATT);
   END LOOP;
   /* ------------------------- CALCULO DO DNPCal --------------------------- */
   IF P_IDNDPCAL = 'S' THEN
      /* ---------------- GRAVA O VALOR DE PROTEINA LIQUIDA (NPU) ----------------- */
      UPDATE
         NUTRTRAB
      SET
         QTPROTLIQU = (SELECT
                          NVL(MAX(VRFATCALNDPC) * QTNUTRCALC,0)
                       FROM
                          NDPCAL
                       WHERE
                          CDPRODUTO = (SELECT
                                          MAX(NDPCAL.CDPRODUTO)
                                       FROM
                                          NDPCAL
                                          , PRODUTO PROD_NDPCAL, PRODUTO PROD_NUTRTRAB
                                       WHERE 
                                          NDPCAL.CDPRODUTO = PROD_NDPCAL.CDPRODUTO AND
                                          PROD_NUTRTRAB.CDPRODUTO = NUTRTRAB.CDPRODNUTR AND
                                          PROD_NUTRTRAB.CDARVPROD LIKE PROD_NDPCAL.CDARVPROD || '%'
                                          ---INSTR(NUTRTRAB.CDPRODNUTR,CDPRODUTO) = 1
                                      )
                       )
      WHERE
         CDNUTRCALC = (SELECT MAX(CDPROTEINA) FROM PARAMGERAL) AND
         NRSEQUNUTR = P_NRSEQUNUTR;
      /* --------------------- GRAVA O VALOR DE CALORIAS -------------------- */
      UPDATE
         NUTRTRAB
      SET
         VRCALORNUTR = QTNUTRCALC
      WHERE
         CDNUTRCALC = (SELECT MAX(CDCALORIAS) FROM PARAMGERAL) AND
         NRSEQUNUTR = P_NRSEQUNUTR;
   END IF;
END;
/