我正在尝试将 MS SQL 函数转换为 oracle,但遇到了麻烦。他们的理由是在函数中创建临时表以逐渐添加。我似乎无法用光标替换临时表以逐渐添加。必须有人知道如何在 Oracle 中编写此代码:
ALTER FUNCTION [dbo].[F_GetDateIntervalTable]
(
@OccurredFrom datetime,
@OccurredTo datetime,
@Interval decimal
)
RETURNS @Tbl table
(
[Dts] datetime
)
AS
BEGIN
DECLARE @Count int
--DECLARE @Tbl table([Dts] datetime)
DECLARE @Dts datetime
DECLARE @SeedDts datetime
SET @Count = 1
SET @Dts = DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE,0,@OccurredFrom)/@Interval)*@Interval, 0);
SET @SeedDts = DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE,0,@OccurredFrom)/@Interval)*@Interval, 0);
SET @OccurredTo = DATEADD(MINUTE, -@Interval, @OccurredTo);
WHILE (@SeedDts < @OccurredTo)
BEGIN
SET @SeedDts = DATEADD(MINUTE, @Interval*(@Count-1), @Dts)
INSERT INTO @Tbl(Dts) VALUES(@SeedDts)
SET @Count = (@Count + 1)
END
RETURN
END
输出应该是这样的(给定参数):
@OccurredFrom = '2013-01-01',
@OccurredTo = '2013-01-02',
@Interval = 60
2013-01-01 00:00:00.000
2013-01-01 01:00:00.000
2013-01-01 02:00:00.000
2013-01-01 03:00:00.000
2013-01-01 04:00:00.000
2013-01-01 05:00:00.000
2013-01-01 06:00:00.000
2013-01-01 07:00:00.000
2013-01-01 08:00:00.000
2013-01-01 09:00:00.000
2013-01-01 10:00:00.000
2013-01-01 11:00:00.000
2013-01-01 12:00:00.000
2013-01-01 13:00:00.000
2013-01-01 14:00:00.000
2013-01-01 15:00:00.000
2013-01-01 16:00:00.000
2013-01-01 17:00:00.000
2013-01-01 18:00:00.000
2013-01-01 19:00:00.000
2013-01-01 20:00:00.000
2013-01-01 21:00:00.000
2013-01-01 22:00:00.000
2013-01-01 23:00:00.000
任何想法都非常感谢!