CREATE OR REPLACE PROCEDURE INCLUI_DADOS_EXPEDICAO
(P_CDFILIAL    IN EXPEDICAO.CDFILIAL%TYPE,
 P_NREXPEDICAO IN EXPEDICAO.NREXPEDICAO%TYPE,
 P_DTEXPEDICAO IN EXPEDICAO.DTEXPEDICAO%TYPE,
 P_TIPOEXP     IN EXPRELACITEMPPED.IDTIPOEXP%TYPE,
 P_CDOPERADOR  IN OPERADOR.CDOPERADOR%TYPE,
 P_NRPEDIDO    IN ITEMPEDIDO.NRPEDIDO%TYPE,
 P_NRITEMPED   IN ITEMPEDIDO.NRITEMPED%TYPE,
 P_QTITEMEXP   IN EXPRELACITEMPPED.QTITEMEXP%TYPE)
AS
   V_EXP INTEGER;
BEGIN
   /*INSERE EXPEDICAO*/
   SELECT COUNT(1)
     INTO V_EXP
     FROM EXPEDICAO
    WHERE CDFILIAL = P_CDFILIAL
      AND NREXPEDICAO = P_NREXPEDICAO;
   IF (V_EXP = 0) THEN
      INSERT INTO EXPEDICAO
      (CDFILIAL, NREXPEDICAO, DTEXPEDICAO, IDSTATUSSEP, CDOPERADOR)
      VALUES
      (P_CDFILIAL, P_NREXPEDICAO,  P_DTEXPEDICAO, 'P', P_CDOPERADOR);
   END IF;

   /*INSERE EXPRELACITEMPPED*/
   INSERT INTO EXPRELACITEMPPED
   (CDFILIAL, NREXPEDICAO, NRPEDIDO, NRITEMPED, IDTIPOEXP, QTITEMEXP)
   VALUES
   (P_CDFILIAL, P_NREXPEDICAO,  P_NRPEDIDO, P_NRITEMPED, P_TIPOEXP, P_QTITEMEXP);
END;
/