Do you know that cursors' performance is nearly the same to the set-based statements when they are run on up to about 1000 rows. In your case 2 million rows for a cursor will burn the cpu.You should find a set-based solution, which is not complex for your case. You can use temp tables or CTEs...

Igor Micev (3/14/2014)Do you know that cursors' performance is nearly the same to the set-based statements when they are run on up to about 1000 rows.

Not really, it might look that way with small sets of data, but it's mostly perception. A set based query will run several times faster than a cursor in most cases.

[/quote]It depends how many columns you're fetching into the cursor and what operations are then perform. However, for one or two columns and small number of rows I haven't seen significant differences. Once I was making some measures, and found out that number (1000) empiricallyl; but as I say, it depends.Yes, set-based is faster in any case.

Igor Micev (3/14/2014)Do you know that cursors' performance is nearly the same to the set-based statements when they are run on up to about 1000 rows.

Not really, it might look that way with small sets of data, but it's mostly perception. A set based query will run several times faster than a cursor in most cases.

It depends how many columns you're fetching into the cursor and what operations are then perform. However, for one or two columns and small number of rows I haven't seen significant differences. Once I was making some measures, and found out that number (1000) empiricallyl; but as I say, it depends.Yes, set-based is faster in any case.[/quote]

It is the small number of rows that concerns me here. We don't generally design new processes against tables with millions of rows but we intend to use them there. And even if the table has only a couple hundred rows today, in the future it can grow to millions. Why write code that is just a time bomb? Your argument sounds like the pleas of somebody who was just asked why they wrote a cursor instead of set based solution. Just do it right the first time and then there is no need to worry about it.