create or replace FUNCTION LISTA_NOTAS_EXPEDICAO(
   P_CDFILIAL     IN EXPEDICAO.CDFILIAL%TYPE,
   P_NREXPEDICAO  IN EXPEDICAO.NREXPEDICAO%TYPE) RETURN VARCHAR2 IS

   V_RESULT       VARCHAR2(5000);
BEGIN
   V_RESULT := '';
   FOR C_PEDIDOS IN (SELECT DISTINCT I.CDFILIALPED, I.NRPEDIDO, N.NRLANCTONF
                       FROM NOTAFISCAL N, ITEMPRODUTO I
                      WHERE N.CDFILIAL = P_CDFILIAL
                        AND N.NREXPEDICAO = P_NREXPEDICAO
                        AND N.CDFILIAL = I.CDFILIAL
                        AND N.NRLANCTONF = I.NRLANCTONF)
   LOOP
      IF (LENGTH(V_RESULT) > 0) THEN
         V_RESULT := V_RESULT || ' - ' || C_PEDIDOS.NRLANCTONF;
      ELSE
         V_RESULT := C_PEDIDOS.NRLANCTONF;
      END IF;
   END LOOP;
   RETURN(V_RESULT);
END;
/