IF EXISTS (SELECT *
             FROM SYSOBJECTS O, SYSUSERS U
            WHERE O.NAME = 'CTB_CCFORNECEDOR'
            AND TYPE = 'P'
            AND U.UID = O.UID
            AND U.NAME = USER)
DROP PROCEDURE CTB_CCFORNECEDOR
GO
CREATE PROCEDURE CTB_CCFORNECEDOR(
   @P_CDFORNECEDOR VARCHAR(21),
   @P_CDFILIAL VARCHAR(4),
   @P_CDEMPRESA VARCHAR(2),
   @P_CDCONTCTBL VARCHAR(20) OUTPUT
)
AS
BEGIN
   DECLARE @V_CDCONTCTBL VARCHAR(20);


   SELECT @V_CDCONTCTBL = CDCONTCTBL
     FROM FORNECEDOR
    WHERE CDFORNECED = @P_CDFORNECEDOR;

   IF @V_CDCONTCTBL IS NULL BEGIN
      SELECT @V_CDCONTCTBL = CDCONFORNFI
        FROM PARAMFILIAL
       WHERE CDFILIAL = @P_CDFILIAL;
   END;

   IF @V_CDCONTCTBL IS NULL BEGIN
      SELECT @V_CDCONTCTBL = CDCONFORNEM
        FROM PARAMEMPRESA
       WHERE CDEMPRESA = @P_CDEMPRESA;
   END;

   IF @V_CDCONTCTBL IS NULL BEGIN
      SELECT @V_CDCONTCTBL = CDCONTFORN
        FROM PARAMCTBL;
   END;

  SET @P_CDCONTCTBL = @V_CDCONTCTBL;
END;
GO

-- *VERSAO* --
EXEC ATUVEROBJ 'CTB_CCFORNECEDOR','5.50.000';
GO


