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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Cursors, Temp Tables?! What to do?!!?!

Author  Topic 

si_g
Starting Member

4 Posts

Posted - 2005-02-09 : 06:36:41
Hi,
I'm having trouble deciding what to do.

Originally, I wrote a stored proc. using a cursor to sift through data and write to a table.

The general concensus I have read is that cursors are bad.
I read some things about maybe using temporary tables instead but im a bit stuck.
Here is an example of my problem in a nutshell.

I have a resultset with 10 rows in it.

For each row, I need to analyse the data and do different updates depending on what data I have.

So, it's basically a 'For Loop' style thing.
With a cursor this is obviously easy and using a recordset in VB then calling a Stored Proc. for each record is also easy but what are the alternatives?

I need enlightening!!!

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-02-09 : 07:32:04
Look at CASE in BOL

Example
UPDATE MyTable
SET Col1 = CASE WHEN Col2 = 1 THEN 0 END

Or with ELSE
UPDATE MyTable
SET Col1 = CASE WHEN Col2 = 1 THEN 0 ELSE 2 END

Andy
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-09 : 09:48:04
you need to give us more info. Often, when you replace cursors, it should be the overall algorithm that need re-writing. Just replacing the cursor code and putting in equivalent non-cursor code (i.e., replacing the cursor with a loop through a temp table) has basically no effect.

It is the act of rethinking about what exactly needs to be done, and re-thinking your algorithm to get things done, and then re-writing it to deal with the multiple rows at the same time that gives you the benefit.



- Jeff
Go to Top of Page
   

- Advertisement -