CREATE OR REPLACE FUNCTION BUSCA_CCUSTO_A(P_CDFILIAL IN VARCHAR, P_CDEMPRESA IN VARCHAR) RETURN VARCHAR
IS
   V_MODELOA    VARCHAR2(20);
   V_CDCENTCUST VARCHAR2(04);
BEGIN
   BEGIN
      SELECT CDCENTCUST
        INTO V_CDCENTCUST
        FROM FILIAL
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDCENTCUST := NULL;
   END;
   IF V_CDCENTCUST IS NULL THEN
      BEGIN
         SELECT CDCENTCUST
           INTO V_CDCENTCUST
           FROM EMPRESA
          WHERE CDEMPRESA = P_CDEMPRESA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCENTCUST := NULL;
      END;
   END IF;
   BEGIN
       SELECT CDCENTCUSTINTE
         INTO V_MODELOA
         FROM CENTCUST
        WHERE CDCENTCUST = V_CDCENTCUST;
   EXCEPTION
        WHEN NO_DATA_FOUND THEN
       V_MODELOA := NULL;
   END;

   RETURN(NVL(V_MODELOA,V_CDCENTCUST));
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('BUSCA_CCUSTO_A','6.39.005'); END;
/
