I have a scalar function which I am converting to TVF.
In the scalar function I have the below while
statement:
WHILE @ReviewDueDate < getdate()
SET @ReviewDueDate = DATEADD(DAY, 30, @ReviewDueDate)
When I put that in the TVF, I use CROSS APPLY
(got some help in converting) and it gets converted to the below code:
CROSS APPLY (
VALUES(DATEADD(DAY, 30 * CEILING(( IIF(CAST(GETDATE() AS TIME) > CAST(CA1.ReviewDueDate AS TIME), 1, 0) + DATEDIFF(DAY, CA1.ReviewDueDate, GETDATE()) ) / 30.0), CA1.ReviewDueDate))
) CA2(ReviewDueDate)
I don't clearly understand the conversion part.
I understand that when @ReviewDate < getdate()
, 30 days needs to be added to the @ReviewDate
.
I am kind of lost when I try to understand the converted CROSS APPLY
code.
Now the hard part for me is:
I have two more while loops as below:
WHILE @ReviewDueDate < getdate()
SET @ReviewDueDate = DATEADD(Month, 6, @ReviewDueDate)
WHILE @ReviewDueDate < getdate()
SET @ReviewDueDate = DATEADD(YEAR, 1, @ReviewDueDate)
So I have to convert it to use it in CROSS APPLY
.
I am not clear how the find while loop is converted, so I am struggling with the other two.
Any help in explaining the first conversion is appreciated.
If someone can help converting the other two, that would be helpful too.