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
 General SQL Server Forums
 New to SQL Server Programming
 Alternative to cursor in SQL SERVER 2005

Author  Topic 

reacha
Starting Member

49 Posts

Posted - 2010-10-13 : 15:11:37
I have a cursor

declare cursor_AUDIT_MetricsInter cursor
fast_forward READ_ONLY
For select pkgid from AUDIT_MetricsInterp
where AuditStampCompleted is null

open cursor_AUDIT_MetricsInter

fetch next from cursor_AUDIT_MetricsInter into @pkgid


but i thought of using temporary variable

insert into @temp2(pkgid)
select pkgid from AUDIT_MetricsInterp
where AuditStampCompleted is null

In this temp variable how can i go thorugh the next record

Please help me out!!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-13 : 15:12:36
We need to find out what you intend to do with each row in order to determine what solution to use.


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

reacha
Starting Member

49 Posts

Posted - 2010-10-13 : 15:25:31
i need to go though each and every pkgid and need to pass that pkgid into 2 procedures and 1 function and get the other column values from there and after than in this main procedure where i am using cursor needs to update the table with the values returned by procedure for each and every pkgid


exec [msp_UseridAuditStampCompleted]@pkgid,@AuditStampCompleted = @AuditStampCompleted output,@userID = @userid output

exec [msp_EventIDAuditStampQueueEntered]@pkgid,@AuditstampQueueEntered = @AuditstampQueueEntered output,@QueueEventID = @QueueEventID output

set @AuditStampLocked = (SELECT [dbo].[mfun_AuditStampLocked] (@pkgid))


UPDATE AUDIT_MetricsInterp
SET USERID = @userid,QueueEventID = @QueueEventID,AuditStampLocked=@AuditStampLocked,
AuditStampCompleted = @AuditStampCompleted,AuditstampQueueEntered=@AuditstampQueueEntered
WHERE PKGID = @pkgid
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-13 : 15:27:02
Then you should just use the cursor.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

reacha
Starting Member

49 Posts

Posted - 2010-10-13 : 15:32:21
No other alternative way because i have one and half million records means pkgid's means it is going to each and every pkgid and getting data.

just to update 9000 records it is taking 45min

How can i avoid the cursor from doing dirty reads
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-13 : 15:36:03
You have to loop to do what you want. Looping is done through a cursor or using WHILE, however they'll have the same performance.

The alternative is to modify your stored procedures so that they can handle batches of rows and not just one row at a time. That's the only way you are going to get this thing to be faster.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -