SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


There Must Be 15 Ways To Lose Your Cursors… Part 2: Just Put It in a S


There Must Be 15 Ways To Lose Your Cursors… Part 2: Just Put It in a S

Author
Message
Samuel Vella
Samuel Vella
SSC Eights!
SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)

Group: General Forum Members
Points: 827 Visits: 2144
jwheeler (4/27/2009)

Then surely the manager hasn't done their job by employing the wrong tool for the task?

If you have database development to be done you either employ a db dev or a C# / VB developer who is also a SQL dev. You don't buy a hammer to put a screw in the wall, surely?


HR could well have given you the hammer and told you to get on with it
Internal shuffling is one way of minimising redundancies
atoth
atoth
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 106
Hi

I still find I need Cursors when Sending out batches of Email notifications from the Server.

For example:

1. A scheduled Job selects records of people who need to be informed of some activity that has occurred - relevant data such as their email address, Name, the description of the activity etc is stored in variables using a CURSOR.

2. Inside the while loop I merge the data from the variables into an HTML Email template, and then send it out using dbMail.

I would love to replace this with set based handling but I can imagine how...particulary step 2

Adam
Samuel Vella
Samuel Vella
SSC Eights!
SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)

Group: General Forum Members
Points: 827 Visits: 2144
atoth (4/27/2009)

I still find I need Cursors when Sending out batches of Email notifications from the Server.

I would love to replace this with set based handling but I can imagine how...particulary step 2

Adam


you're performing a procedural not a set based operation and the "per-row" function calls means that you will need to use a cursor.

Cursors do have their uses and this is one of them.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86758 Visits: 41103
Samuel Vella (4/27/2009)
atoth (4/27/2009)

I still find I need Cursors when Sending out batches of Email notifications from the Server.

I would love to replace this with set based handling but I can imagine how...particulary step 2

Adam


you're performing a procedural not a set based operation and the "per-row" function calls means that you will need to use a cursor.

Cursors do have their uses and this is one of them.


Heh... in SQL Server 2005, even that can be done without a cursor. We're just gonna have to wait for it in Barry's series of articles.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
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

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
george sibbald
george sibbald
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10392 Visits: 13687
nice work barry, you must have slaved other this. You should make a book out of all the chapters once you are done, as Lynn said it would become required reading.

I particularly like the rules for converting code to set based, useful mental prompts. Also the stats gathering method, although not applicable if using SQL 2000.

---------------------------------------------------------------------
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23663 Visits: 9730
Another good one.

And on whatever the medical situation is, get well soon, man.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Kit G
Kit G
SSChasing Mays
SSChasing Mays (608 reputation)SSChasing Mays (608 reputation)SSChasing Mays (608 reputation)SSChasing Mays (608 reputation)SSChasing Mays (608 reputation)SSChasing Mays (608 reputation)SSChasing Mays (608 reputation)SSChasing Mays (608 reputation)

Group: General Forum Members
Points: 608 Visits: 4113
Neat article and nicely written. I'm new to SQL but it's great to have this reference on how to change that procedural thought into a set thought.

By the way, the link to Part 1 is pointing to Part 2.

Good job and get well soon! Your fans eagerly await the next installment. Smile

-- Kit
Samuel Vella
Samuel Vella
SSC Eights!
SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)SSC Eights! (827 reputation)

Group: General Forum Members
Points: 827 Visits: 2144
Jeff Moden (4/27/2009)


Heh... in SQL Server 2005, even that can be done without a cursor. We're just gonna have to wait for it in Barry's series of articles.


OK it *can* be done without a cursor... I have seen some dirty work arounds to get stored procedure calls inside select statements using linked servers but they need certain security features turned on and would still end up as a row by row operation when processed within the database engine (not to mention all the other overheads associated with opening new DB connections) so no performance benefit.

IMO a last resort technique (even after the possibility of SSIS has been excluded).
jwheeler-985942
jwheeler-985942
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 31
Writing a .net app to pull the dataset and iterate through it sending the mails would be faster than doing it in SQL, especially as merging the html template will involve some substring / replace activities (which in volume SQL isn't very fast at).

Depends on your volumes of course, if you are sending only a few thousand at a time a cursor or while loop would probably be fine.
sknox
sknox
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2622 Visits: 2832
jwheeler (4/27/2009)
Samuel Vella (4/27/2009)
jwheeler (4/27/2009)
The fact you even have to tell so called developers this stuff is frightening.


Not really and it's something I've seen a few times...

When someone has a background in VB or C and has been dumped into SQL development then they might be able to write code but they lack the set based thinking which a good DB developer needs (they might not even be aware of being able to return sets depending on how much exposure they're had to databases in their previous life).

Usually a helping hand with their first few pieces of work will get them thinking the right way from the outset.

I know from experience when dabbling in ASP .NET that something which I've spent hours and several tens of lines of code trying to get to work can be ripped apart and rewritten as a very simple class by someone who knows what they're doing



Then surely the manager hasn't done their job by employing the wrong tool for the task?

If you have database development to be done you either employ a db dev or a C# / VB developer who is also a SQL dev. You don't buy a hammer to put a screw in the wall, surely?


That's a grossly oversimplified depiction. For a more realistic scenario, consider the following:

You're an IT manager for a medium-sized business, and you need a developer to build a custom database application -- but you've got almost no budget. You finally manage to convince the accountant to let you hire ONE developer.

Since you've got no budget, you do a little research and find the Visual Studio Express editions. You want to have some understanding of the created code, and you have some VB background. Now you've got a databse (SQL Server Express), a development environment (Visual Studio Express) and a development language (Visual Basic). So you put those requirements into your favorite jobs website and wait for your prince to come.

All of the responses you get back are specialized developers, SQL devs or VB devs who don't have a lot of db experience. What do you do?

From this (much more realistic) scenario, you should see that the problem is more like having a square bolt, and only having closed-end metric and standard hex wrenches. Sometimes none of the tools available to you fit the job quite right, and you have to pick the one that comes closest. Sometimes there's a tool you already have which isn't the right one but is a lot more easy to justify than spending $80k on a new tool for one job.

Of course the problem with all of these analogies is that we developers always come out looking like tools. Ermm
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search