Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Site Related Forums
 The Yak Corral
 I love cursors

Author  Topic 

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2003-11-18 : 09:59:13
I put this here, because I don't have question.

Today I had to a lot of difficult stuff on my application database.

Sometimes Cursors are better than set based programming.

Do you know why?

Because to make a smart SET based solution is sometimes to difficult for me. Breaking down things step by step is sometimes a better solution, than to do it smart en clever.

I just wanted to share this. Using a cursor always gives me the feeling i was dumb (because of this forum, that is )

Now I feel better, hehe

Henri

~~~
Success is the ability to go from one failure to another with no loss of enthusiasm

X002548
Not Just a Number

15586 Posts

Posted - 2003-11-18 : 10:04:39
quote:
Originally posted by henrikop

Breaking down things step by step is sometimes a better


Yeah...that's how I (try to) build set based...

Start small, confirm results and build...

Hey, what ever floats your boat...and if batch windows are of no concern, don't worry when (if) they close...



Brett

8-)
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2003-11-18 : 17:32:12
quote:

Because to make a smart SET based solution is sometimes to difficult for me.



So what you are saying is that cursors are better because set based is too hard ?
That doesn't make any sense to me. That's like saying a volkswagon is faster than a Ferarri because you will never be able to drive a Ferarri.



Damian
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-11-18 : 17:42:53
I agree with henrikop
It's why I can charge a lot of money.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-11-18 : 18:53:33
Yes! And I agree with henrikop.
An excellent example of what he means is here.

Of course it looks very 'coool' when a newbie publicly announces
his tender love to the set-based approach and his spitful hatred
to the cursors... OK, I don't mind... I just wonder: how long it
will take for the newbie to write my set-based query from
the mentioned above link??
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-11-18 : 19:36:37
quote:
I just wonder: how long it will take for the newbie to write my set-based query from the mentioned above link??
How long would it take for them to post the question on SQL Team?

Seriously, everyone seems to think that they have to learn set-based operations in a vacuum, with no help, guidance, or direction. I've said this before in previous discussion/flamewars on cursors: look at the number of set-based solutions that have been delivered by SQL Team, for people who didn't believe or downright INSISTED it couldn't be done without using cursors. It's not even A CHALLENGE for us anymore.

If someone doesn't want to learn a better way to do things, that's their business, but it does not mean that the learning is a waste of time. No one on this site, top 10 posters included, was born a set-based wizard. It's an acquired skill, and one definitely worth whatever effort is needed to acquire.
Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-11-18 : 20:17:45
Yes, Rob :)
Though here is a slightly prolonged misunderstanding (guess due to
my damned 'linguistic' handicap) I swear I can sign under each your
word.
But.. aahhhh... there is a terrible factor - the factor of Time and
its everlasting lack.
Deadlines, bad mood, headache, "do it in 15 minutes!" and so on...
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2003-11-18 : 20:23:16
Well said Rob,

But Whats that old saying,

"When the only tool you have is a hammer.... The whole world looks like a cursor"

No thats not it... Hmmm... How does that go??
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-11-18 : 20:42:02
quote:
But.. aahhhh... there is a terrible factor - the factor of Time and
its everlasting lack.
Perhaps, but think about this: it takes months to learn how to swim, but only 60 seconds to drown.
Go to Top of Page

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2003-11-18 : 20:45:25
Thx Stoad,

I love the beautie of SET-BASED, and yes there's SQLTeam to help me out. But I can give some examples which makes it really really hard to do it set-based. Expecially which transactions, XML schema's BizTalk server etc. etc. a cursor is a good and most of the time solid solution. Though 90% of the time I use set-based query's, and SQLTeam helped me to learn a lot.



Henri

~~~
Success is the ability to go from one failure to another with no loss of enthusiasm
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-11-18 : 21:05:08
quote:
Expecially which transactions, XML schema's BizTalk server etc. etc. a cursor is a good and most of the time solid solution
Geez, you might as well combine nitroglycerine, anthrax, and plutonium, they'll do less damage.

Putting a cursor inside a transaction is like breaking a racehorse's legs during the race...then pouring cement around them...and hanging an anvil around its neck...after you cut its head off.

And please don't think that BizTalk is the be-all end-all of app development. It has a habit of creating very generic applications, but not high-performing ones. They might work with anything, but they don't work WELL with anything. If you have no choice but to use it, well...damn I feel sorry for ya.

Set-based is not the blocking factor for you here, but BizTalk.
Go to Top of Page

henrikop
Constraint Violating Yak Guru

280 Posts

Posted - 2003-11-19 : 03:28:58
Hahaha,

I *dislike* BizTalk (to say the least), and I feel sorry for myself because it doesn't perform at all. But don't worry about the cursor I have my own way of handling transactions and secure them. I did not choose BizTalk, I was jumping on a riding train. So thx for bearing with me .

I haven't got a lot of time, but I'll be back showing why I use cursors every now and then...




Henri

~~~
Success is the ability to go from one failure to another with no loss of enthusiasm
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-11-19 : 07:57:35
True story:

I first learned SQL in general by using Access. I had no idea there was any way OTHER than via SELECT, UPDATE and INSERT queries to manipulate & retreive the contents of tables. Because I thought there was no alternative, all I learned were set-based ways of doing things.

I had heard of "cursors" and heard people talk about all the things you could do with them, and was EMBARASSED that I wasn't really familiar with them, and thought "these things must be so advanced and powerful!". Even in CS courses at my school, students discussed using them in C++ and all that, and I had no idea. Finally, years later, I learned what they were and how they work and thought "are you kidding me?".

Sometimes I think that's the best way to learn set-based -- to believe (or pretend) there is no other alternative. Cursors do have their place, and even recently here at SQLTeam I (sort of) defended cursors when I showed a particular speed comparison of the two was not a fair test.

I look forward to seeing some of your examples, please don't hesitate to post your cursor solutions here... and, of course, we won't hesitate to show you how most 100-line cursor routines can be re-written as 1 or 2 line SQL statements!

- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-11-19 : 08:50:08
quote:
Originally posted by jsmith8858

and even recently here at SQLTeam I (sort of) defended cursors when I showed a particular speed comparison of the two was not a fair test.



I didn't think (as always) that we ever finished that "test".

The operation needed to do a look up AND another operation which we never got around to...and I as well was looking, searching, experimenting to see if the Cursor would outperform the set based solution (which it really wasn't anyway, because it was all singleton selects in a loop). It WAS an example of how you don't need a cursor.

Soooooo, in the last year (since I joined) There has not yet been a concrete example of how ANY Cursor outperforms set based....

Where did I put that thread....


Ohh and MOO





Brett

8-)
Go to Top of Page
   

- Advertisement -