Time for some mathematical magic! :)
Breaking the stored procedure down:
- If
@i
is greater than or equal to 714
, it returns nothing; - If
@i
is greater than or equal to 260
, it returns 260
× C
, where C
is the number of records in the VacationGadget
table; - If
@i
is less than 260
, it returns @i
× C
;
With your sample run,
@i
is
528
, and the result is
3,138,460
. Therefore, since
@i
is greater than
260
, we can deduce that
C
=
3,138,460
÷
260
=
12,071
.
Now to the loop.
- We can discount the first iteration, since
0
× anything = 0
. - We can probably discount anything from
714
to 888
- assuming your GetIntFromSQL
method does the right thing when the stored procedure doesn't return any results. - For
1
to 259
, the result from SQL will be i
× C
.
Therefore, the total sum for that part of the loop will be:
(the sum of integers from 1
to 259
) × C
. - For
260
to 713
, the result from SQL will be 260
× C
.
Therefore, the total sum for that part of the loop will be (714
- 260
) × 260
× C
;
Plumbing some numbers in:
- (the sum of integers from
1
to 259
)
= 259
× 260
÷ 2
(Why?[^])
= 33,670
; - (
714
- 260
) × 260
= 454
× 260
= 118,040
;
The final result will be:
33,670
×
C
+
118,040
×
C
=
151,710
×
C
=
151,710
×
12,071
=
1,831,291,410