CREATE OR REPLACE PROCEDURE SOMATORIA_QTDE_PAX_STANDBYM( 
  P_CDFILIAL            IN FILIAL.CDFILIAL%TYPE, 
  P_CDCIA               IN COMPANHIA.CDCIA%TYPE, 
  P_NRVOO               IN VOO.NRVOO%TYPE, 
  P_CDTRECHO            IN VOOTRECHO.CDTRECHO%TYPE, 
  P_CDCLASSE            IN CLASSE.CDCLASSE%TYPE, 
  P_QTTOTPAXAT_QTPAXSBY IN OUT PREVISAOPAXM.QTTOTPAXAT%TYPE, 
  P_QTTOTPAXAT          IN OUT PREVISAOPAXM.QTTOTPAXAT%TYPE, 
  P_QTPAXSBY            IN OUT PREVISAOPAXM.QTTOTPAXAT%TYPE, 
  P_CDEQUIP             IN VOO.CDEQUIP  %TYPE DEFAULT NULL, 
  P_SGPREFIXO           IN VOO.SGPREFIXO%TYPE DEFAULT NULL 
)AS 
  V_CDEQUIP                VOO.CDEQUIP  %TYPE; 
  V_SGPREFIXO              VOO.SGPREFIXO%TYPE; 
BEGIN 
 
 
 
   UPDATE CONFIGVOO 
   SET QTPAXSBYVOO = 0 
   WHERE (QTPAXSBYVOO IS NULL); 
 
   UPDATE CONFIGPREF 
   SET QTPAXSBYPREF = 0 
   WHERE (QTPAXSBYPREF IS NULL); 
 
   UPDATE CONFIGEQUI 
   SET QTPAXSBYEQUI = 0 
   WHERE (QTPAXSBYEQUI IS NULL); 
 
 
 
 
   SELECT MAX(QTPAXVOO), MAX(QTPAXSBYVOO) 
   INTO P_QTTOTPAXAT, P_QTPAXSBY 
   FROM CONFIGVOO 
   WHERE (CDFILIAL = P_CDFILIAL) AND 
         (CDCIA    = P_CDCIA   ) AND 
         (NRVOO    = P_NRVOO   ) AND 
         (CDTRECHO = P_CDTRECHO) AND 
         (CDCLASSE = P_CDCLASSE); 
 
   IF (P_QTTOTPAXAT + P_QTPAXSBY) IS NULL THEN 
   BEGIN       
      V_CDEQUIP   := P_CDEQUIP; 
      V_SGPREFIXO := P_SGPREFIXO;
       
      SELECT MAX(QTPAXPREFIXO),MAX(QTPAXSBYPREF) 
      INTO P_QTTOTPAXAT, P_QTPAXSBY 
      FROM CONFIGPREF 
      WHERE (CDFILIAL  = P_CDFILIAL ) AND 
            (CDCIA     = P_CDCIA    ) AND 
            (CDCLASSE  = P_CDCLASSE ) AND 
            (SGPREFIXO = V_SGPREFIXO); 
      IF (P_QTTOTPAXAT + P_QTPAXSBY) IS NULL THEN 
         SELECT MAX(QTPAXEQUI),MAX(QTPAXSBYEQUI) 
         INTO P_QTTOTPAXAT, P_QTPAXSBY 
         FROM CONFIGEQUI 
         WHERE (CDFILIAL = P_CDFILIAL) AND 
               (CDCIA    = P_CDCIA   ) AND 
               (CDCLASSE = P_CDCLASSE) AND 
               (CDEQUIP  = V_CDEQUIP ); 
      END IF; 
       
   END; 
   END IF; 
   P_QTTOTPAXAT_QTPAXSBY := P_QTTOTPAXAT + P_QTPAXSBY; 
END; 
/