January 10, 2007 at 11:44 pm
Hi All,
Quite often we are advised not to use a cursor, and try to find alternative methods instead. Since I'm a learner, I get a bit confused as to what other methods can their be?
Could you guys have a look at the following code for example - and advise what other methods it can be written in?
Thanks heaps in advance.
declare member_account cursor for
select portfolio_id, portfolio_name, apir_code, class_name, class_code
from #OA_portfolio_name
for read only
open member_account
fetch member_account into @portfolio_id, @portfolio_name, @apir_code, @class_name, @class_code
while ( @@sqlstatus != 2 )
begin
exec sp_OA_portfolio_earnings @member_account_id,.....
insert into #OA_port_percentage values (
@member_account_id,
@portfolio_id,.....
)
/* reset the values */
select @start_units = 0,.....
fetch member_account into @portfolio_id, @portfolio_name, @apir_code, @class_name, @class_code
end
CLOSE member_account
DEALLOCATE member_account
January 10, 2007 at 11:58 pm
hi
Methods generally used to avoid cursors are
using temporary tables,table data types, other looping syntaxes and implementing a different logic if possible.
In many cases i have seen that using a different logic instead of cursors gives a good performance boost.
In this u can u cud try storing member info in a temp table and use a while loop .
"Keep Trying"
January 11, 2007 at 12:52 am
The best solution for replacing Cursors is by using SET based logic. While you may see some performance gains by using temp tables or table variables with while loops, these are still procedural in nature and SQL Server was designed to work with sets of data. Working one row at a time is a mindset that many new SQL Server developers and DBAs have a hard time breaking. For some good exapmles, search this site for RBAR (Row by
January 11, 2007 at 1:36 am
the while loop is the alternative, although from a sql point of view a cursor and a while loop hold the transactions differently ( by default ).
It's a pain dealing with row based ops but sometimes thats what you need, fast forward read only ( or something similar ) are best for reads. I don't very often use cursors but as a simple example stepping through index rebuilds is a good example where I do.
Derived tables can be very useful.
[font="Comic Sans MS"]The GrumpyOldDBA[/font]
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
January 12, 2007 at 12:27 am
Thanks guys. I'll try that. I also found a really cool example at http://www.sql-server-performance.com/dp_no_cursors.asp
Cheers
January 12, 2007 at 2:42 am
whilst I agree the while loop replaces the cursor the operation is still the same and while loops often do not run better than cursors. The real answer is to eliminate row based operations.
[font="Comic Sans MS"]The GrumpyOldDBA[/font]
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
January 12, 2007 at 4:33 am
couldn't agree with you any more. What do you suggest then?
June 14, 2007 at 6:35 pm
Sorry I'm late with this reply...
The answer is...
.... rewrite sp_OA_portfolio_earnings to handle more than one row at a time.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply