|
Posted by Ed Murphy on 08/22/06 06:01
On Mon, 21 Aug 2006 08:05:49 +0000 (UTC), Erland Sommarskog
<esquel@sommarskog.se> wrote:
>Ed Murphy (emurphy42@socal.rr.com) writes:
>> What are the pros and cons of relying on such a table vs. using a
>> WHILE loop? Based on Rob's context of student registrations, let's
>> assume we're talking about a maximum of 300 iterations per row in
>> the original tblDates table.
>
>The one risk with a table of numbers is that if you run of numbers, you
>will get an incorrect result. That is one reason why I'm reluctant to
>use it, if there are alternative solutions. But for a case like this,
>when you need to fill up a space, a table of numbers - or dates - is what
>you need.
>
>A loop is more complex to program, and easier go wrong.
I disagree, but then I have somewhat more experience with imperative
than functional programming. Consider:
x = first_date
while x <= last_date
insert x, datediff(x, mid_date) into <table>
x = dateadd(x, 1)
end while
versus
select dateadd(first_date, n), n - datediff(mid_date, first_date)
into <table>
from numbers
where n between 0 and datediff(end_date, first_date)
Okay, "where n between <limits>" makes sense as an analogue to a while
loop, but that stuff in line 1 looks like the stuff of headaches.
> And as a generic
>solution, you face scalability problems.
I kind of figured. The query seems easy to get wrong, though, if
you're not familiar with the pattern; I first wrote it as "where
dateadd(first_date, n) between first_date and last_date", but that
seems like it'd be a good bit slower.
[Back to original message]
|