gerar séries sybase

/* Sybase 9 / Sql Anyware */
CREATE PROCEDURE generate_series(in @BeginData DateTime, in @EndDate DateTime, in @DatePart varchar(32) DEFAULT '1 day')
RESULT (item Datetime)
BEGIN
   DECLARE LOCAL TEMPORARY TABLE @Serie (
      item Datetime
   )ON COMMIT PRESERVE ROWS;
    
   declare @arg varchar(32); 
   declare @len integer; 
   declare @pos integer; 
    
   declare @Data DateTime;
   declare @DatePartType varchar(11);
   declare @DatePartIncrement integer;
    
   SET @arg = TRIM(@DatePart);
   SET @pos = CHARINDEX(' ', @arg );
   SET @len = LENGTH(@arg);
   SET @DatePartIncrement = CAST(LEFT(@arg, @pos) as integer);
   SET @DatePartType = RIGHT(@arg, @len - @pos);
   SET @Data = @BeginData;

   WHILE (@Data <= @EndDate) LOOP
      print('Data ' ||  @Data);
      INSERT INTO @Serie(item) VALUES( @Data );
      SET @Data = CASE 100 
      WHEN SIMILAR( @DatePartType, 'year' ) THEN dateadd(year , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'quarter' ) THEN dateadd(quarter , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'month' ) THEN dateadd(month , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'day' ) THEN dateadd(day , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'week' ) THEN dateadd(week, @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'hour' ) THEN dateadd(hour , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'minute' ) THEN dateadd(minute , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'second' ) THEN dateadd(second , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'millisecond' ) THEN dateadd(millisecond , @DatePartIncrement, @Data )
      ELSE dateadd( day, @DatePartIncrement, @Data ) END                         
   END LOOP;   
    
   SELECT * FROM @Serie;
   -- SELECT * FROM vw_generate_series_TEST
END;
/* View com Testes */
ALTER VIEW vw_generate_series_TEST as 
SELECT 1 as Test, '2001-01-01 00:00:00.000' as BeginDateArg, '2010-12-31 00:00:00.000' as EndDateArg, '1 year       ' as DatePartArg, 10   OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 2 as Test, '2010-01-01 00:00:00.000' as BeginDateArg, '2010-12-31 00:00:00.000' as EndDateArg, '1 quarter    ' as DatePartArg,  4   OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 3 as Test, '2010-01-01 00:00:00.000' as BeginDateArg, '2010-12-31 00:00:00.000' as EndDateArg, '1 month      ' as DatePartArg, 12   OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 4 as Test, '2010-01-01 00:00:00.000' as BeginDateArg, '2010-01-31 00:00:00.000' as EndDateArg, '1 day        ' as DatePartArg, 31   OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 5 as Test, '2010-01-01 00:00:00.000' as BeginDateArg, '2010-01-31 00:00:00.000' as EndDateArg, '1 week       ' as DatePartArg, 5    OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 6 as Test, '2010-01-01 00:00:00.000' as BeginDateArg, '2010-01-01 23:59:59.000' as EndDateArg, '1 hour       ' as DatePartArg, 24   OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 7 as Test, '2010-01-01 23:00:00.000' as BeginDateArg, '2010-01-01 23:59:59.000' as EndDateArg, '1 minute     ' as DatePartArg, 60   OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 8 as Test, '2010-01-01 23:00:01.000' as BeginDateArg, '2010-01-01 23:01:00.000' as EndDateArg, '1 second     ' as DatePartArg, 60   OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 9 as Test, '2010-01-01 23:00:00.001' as BeginDateArg, '2010-01-01 23:00:01.000' as EndDateArg, '1 millisecond' as DatePartArg, 1000 OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 
ORDER BY 1 
Luis Alfredo G Caldas Neto