CREATE OR REPLACE FUNCTION CDFISCAL_ITEM
   (P_CDFILIAL    IN ITEMPRODUTO.CDFILIAL%TYPE,
    P_NRLANCTONF  IN ITEMPRODUTO.NRLANCTONF%TYPE,
    P_NRSEQITPROD IN ITEMPRODUTO.NRSEQITPROD%TYPE,
    P_CDIMPOSTO   IN IMPOSTO.CDIMPOSTO%TYPE) RETURN STRING
AS
   V_CDFISCAL ITEMPROIMPOS.CDFISCALIT%TYPE;

BEGIN
   SELECT MAX(CDFISCALIT)
   INTO V_CDFISCAL
   FROM ITEMPROIMPOS
   WHERE (CDFILIAL     = P_CDFILIAL)    AND
         (NRLANCTONF   = P_NRLANCTONF)  AND
         (NRSEQITPROD  = P_NRSEQITPROD) AND
         (CDIMPOSTO    BETWEEN P_CDIMPOSTO AND P_CDIMPOSTO) AND
         (NRITPROIMPOS = '1');

   RETURN (V_CDFISCAL);
END;

/

