IF EXISTS (SELECT *
             FROM SYSOBJECTS O, SYSUSERS U
            WHERE O.NAME = 'FERIADO_BANCARIO' AND
                  TYPE   = 'FN'               AND
                  U.UID  = O.UID              AND
                  U.NAME = USER)
   DROP FUNCTION FERIADO_BANCARIO
GO

CREATE FUNCTION FERIADO_BANCARIO ( @P_CDFILIAL VARCHAR(04),
                                   @P_DTATUAL  DATETIME
                                  )RETURNS DATETIME
AS
BEGIN
   DECLARE @V_DTBASE DATETIME;
   DECLARE @V_COUNT  DOUBLE PRECISION;

   SET @V_COUNT = 1;
   SET @V_DTBASE = @P_DTATUAL;

   WHILE (@@FETCH_STATUS = 0) BEGIN
      IF (@V_COUNT > 0) BEGIN
         SELECT @V_COUNT = COUNT(*)
           FROM FERIBANC
          WHERE CDFILIAL = @P_CDFILIAL
            AND DTFERIBANC = @V_DTBASE;

         IF @V_COUNT > 0 THEN
            @V_DTBASE = @V_DTBASE + @V_COUNT;
         END;
      END;
   END;
   RETURN (@V_DTBASE);
END;
GO


-- VERSAO --
EXEC ATUVEROBJ 'FERIADO_BANCARIO','5.50.xxx';
GO