CREATE OR REPLACE FUNCTION CTB_CCFORNECEDOR(
   P_CDFORNECEDOR IN FORNECEDOR.CDFORNECED%TYPE,
   P_CDFILIAL IN FILIAL.CDFILIAL%TYPE,
   P_CDEMPRESA IN EMPRESA.CDEMPRESA%TYPE
) RETURN VARCHAR2
AS
   V_CDCONTCTBL VARCHAR2(20);
BEGIN
   BEGIN
      SELECT CDCONTCTBL
        INTO V_CDCONTCTBL
        FROM FORNECEDOR
       WHERE CDFORNECED = P_CDFORNECEDOR;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDCONTCTBL := NULL;
   END;

   IF V_CDCONTCTBL IS NULL THEN
      BEGIN
         SELECT CDCONFORNFI
           INTO V_CDCONTCTBL
           FROM PARAMFILIAL
          WHERE CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCONTCTBL := NULL;
      END;
   END IF;

   IF V_CDCONTCTBL IS NULL THEN
      BEGIN
         SELECT CDCONFORNEM
           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 CDCONTFORN
           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_CCFORNECEDOR','5.50.000'); END;
/
