IF EXISTS( SELECT NAME
             FROM SYSOBJECTS
            WHERE NAME = 'CTB_CCCLIENTE' AND TYPE = 'P')

   DROP PROCEDURE CTB_CCCLIENTE;

GO
CREATE PROCEDURE CTB_CCCLIENTE(
   @P_CDCLIENTE VARCHAR(21),
   @P_CDFILIEMIT VARCHAR(4),
   @P_CDEMPRESA VARCHAR(2),
   @P_CDCONTCTBL VARCHAR(20) OUTPUT)
AS
   DECLARE @V_CDCONTCTBL VARCHAR(20);

BEGIN

   SELECT @V_CDCONTCTBL = CDCONTCTBL
    FROM CLIENTE
    WHERE CDCLIENTE = @P_CDCLIENTE;

  IF @V_CDCONTCTBL IS NULL BEGIN
     SELECT @V_CDCONTCTBL = CDCONCLIEFI
       FROM PARAMFILIAL
      WHERE CDFILIAL = @P_CDFILIEMIT;

  END;

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

  END;

   IF @V_CDCONTCTBL IS NULL BEGIN
      SELECT @V_CDCONTCTBL = CDCONTCLIE
        FROM PARAMCTBL;

   END;

   SET @P_CDCONTCTBL = @V_CDCONTCTBL;

END;
GO

-- VERSAO --
EXEC TEKNISA.ATUVEROBJ 'CTB_CCCLIENTE','5.50.000';
GO









