|
Posted by Erland Sommarskog on 08/22/06 08:12
Ed Murphy (emurphy42@socal.rr.com) writes:
> 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.
Loops are particularly prone to two sorts of errors:
* They goes on forever, could be because of a sloppy mistake, of because the
logic is complicated.
* One-off errors because of incorrect loop conditions.
One-off errors are easy to make with set-based queries as well, but the
risk of infinite loops is nothing you have to lose sleep over.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
[Back to original message]
|