It took me 14 minutes and I developed a solution which turned out to closely approximated one or more of what was on Fritchey's page.
What is wrong with using a WHILE and what would be a preferable approach? Also, did you learn of this in a book or where?
I can tell you... SQL Server is a "set based" / declarative language where you tell it what you want to do to a "set" of information and it figures out (usually) the best way to execute the code. Overriding that with an explicit loop will almost always (notice I said "almost", there are rare instances where this is not true) result in code that's a lot slower and a whole lot more resource hungry.
To be specific, when I'm hiring someone for a database Developer position or even a GUI Developer who will be touching a database a lot (and most do), I'm not looking for a casual database user that will use slow procedural code for such a simple problem. I'm looking for someone who can leverage both the machine and the language. I also want it to be fully commented, very well formatted code because I want to see what their programming style is. There's nothing worse than hiring someone well studied and capable that's either too lazy or too arrogant to write production quality code. In other words, do they embrace the mantra "Make it work, make it fast, make it pretty and it ain't done 'til it's pretty"? People well studied in the art of T-SQL and logical thinking know how to do all 3 without missing a beat.
is pronounced "ree-bar
" and is a "Modenism
" for R
First step towards the paradigm shift of writing Set Based code:
________Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
"If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
"Change is inevitable... change for the better is not."
How to post code problems
How to Post Performance Problems
Create a Tally Function (fnTally)