March 4, 2015 at 12:42 pm
Hi All,
This is NOT a question about when to or not to use loops and cursors, but I have heard from several places that using cursors seems "more intuitive". I have never run across a situation where I couldn't use a loop, nor one where it was harder to understand than a cursor. Is there some history to cursors which lead people to lean towards them over loops?
March 4, 2015 at 1:23 pm
JeeTee (3/4/2015)
Hi All,This is NOT a question about when to or not to use loops and cursors, but I have heard from several places that using cursors seems "more intuitive". I have never run across a situation where I couldn't use a loop, nor one where it was harder to understand than a cursor. Is there some history to cursors which lead people to lean towards them over loops?
Most programmers are coming from C#, C++ world, where imperative code is the rule. SQL is declarative, not an imperative language. So when you're coming from that mind set, it looks "easier" to write a cursor in SQL. The problem is that SQL server does not want you to tell "how to get" the data, but what to get, which is the same definition of a declarative language.
Does that help?
March 4, 2015 at 1:40 pm
JeeTee (3/4/2015)
Hi All,This is NOT a question about when to or not to use loops and cursors, but I have heard from several places that using cursors seems "more intuitive". I have never run across a situation where I couldn't use a loop, nor one where it was harder to understand than a cursor. Is there some history to cursors which lead people to lean towards them over loops?
When they say that cursors are more intuitive, they're usually not talking about when compared to temp tables and WHILE loops. They're talking about when compared to "set based" programming. Now that's a real problem for some because front-end Developers are used to writing a loop (which is also a cursor to me) and don't understand that, behind the scenes, every SELECT is virtually a loop (which I refer to as a "Pseudo-Cursor" with full credit to R.Barry Young for the term). Cursors (and While loops) also support the more common programmatic method of doing everything for a row before moving to the next instead of processing things by whole columns like most set based code does.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2015 at 1:53 pm
I personally hate cursors because the syntax is bloated, counter-intuitive and just plain ridiculous. I have been writing SQL for 15+ years and still need to reference Books online or a previously written cursor when I wrote them in the past. Loops, on the other hand are very simple; I could write a loop in my sleep. If I am reverse engineering someone's code I find loops easy to understand and get sick to my stomach when I need to reverse engineer a cursor. :sick:
So why would someone go through the added frustration of writing a cursor vs a loop? Performance is a huge reason. If you had a requirement for a running total you will find that a well-optimized cursor (e.g. using the FAST_FORWARD option) will blow the socks off a loop. That's really it as far as I'm concerned.
All that said (and I know that you said "This is NOT a question about when to or not to use loops and cursors") I have have found that, once you have practiced it for awhile, the set-based approach is actually very intuitive; usually more so that even a loop. The code is often more concise and so is the query plan it produces.
That's my 2 cents.
-- Itzik Ben-Gan 2001
March 4, 2015 at 1:55 pm
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply