CREATE OR REPLACE FUNCTION NOTA_ORIGEM
          (P_CDFILIAL    IN NOTAFISCAL.CDFILIAL%TYPE,
           P_NRLANCTONF  IN NOTAFISCAL.NRLANCTONF%TYPE) RETURN STRING
AS
   V_RESULT VARCHAR2(15);
BEGIN
   SELECT MAX(NOTAFISCAL.NRNOTAFISC || DECODE(NOTAFISCAL.CDSERIEENTR,'',NOTAFISCAL.CDSERIE,NOTAFISCAL.CDSERIEENTR))
   INTO V_RESULT
   FROM  ITEMNOTAFISC, NOTAFISCAL
   WHERE ITEMNOTAFISC.CDFILIAL     = P_CDFILIAL          AND
         ITEMNOTAFISC.NRLANCTONF   = P_NRLANCTONF        AND
         ITEMNOTAFISC.CDFILIALIT   = NOTAFISCAL.CDFILIAL AND
         ITEMNOTAFISC.NRLANCTONFIT = NOTAFISCAL.NRLANCTONF;

   RETURN(V_RESULT);
END;
/

