CREATE or REPLACE FUNCTION CTB_CCLUCROSPERDAS(
P_CDFILIAL  IN FILIAL.CDFILIAL%TYPE,
P_CDEMPRESA IN EMPRESA.CDEMPRESA%TYPE) RETURN VARCHAR2
AS
   V_CDCONTCTBL VARCHAR2(20);
BEGIN
   BEGIN
      SELECT CDCTBLPERDFI
        INTO V_CDCONTCTBL
        FROM PARAMFILIAL
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDCONTCTBL := NULL;
   END;

   IF V_CDCONTCTBL IS NULL THEN
      BEGIN
         SELECT CDCTBLPERDEM
           INTO V_CDCONTCTBL
           FROM PARAMEMPRESA
          WHERE CDEMPRESA = P_CDEMPRESA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCONTCTBL := NULL;
      END;
   END IF;

   IF V_CDCONTCTBL IS NULL THEN
      BEGIN
         SELECT CDCTBLPERDPA
           INTO V_CDCONTCTBL
           FROM PARAMCTBL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCONTCTBL := NULL;
      END;
   END IF;
  RETURN V_CDCONTCTBL;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('CTB_CCLUCROSPERDAS','5.50.000'); END;
/