Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Cursor.. cmds don’t seem to update until everything has finished Expand / Collapse
Posted Wednesday, March 6, 2013 6:30 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 6, 2015 2:34 AM
Points: 200, Visits: 518
I have a cursor and in that cursor
I have a number of commands like

EXEC (@command)
print 'ghgfhgfhgfh’
insert mytable values (@aaaaa)

I would expect that with each loop of the cursor I would see the results of the print statement and a value inserted into mytable.

But I don’t it seems that the whole cursor has to be processed then all print statements appear and all the inserts occur all at once.

I need to see how far and fast the cursor is processing, also I am worried that none of the memory is being freed up until the whole cursor has finished

Any help?

Many thanks
Post #1427345
Posted Wednesday, March 6, 2013 6:41 AM



Group: General Forum Members
Last Login: 2 days ago @ 9:28 AM
Points: 7,887, Visits: 16,787
Try using this instead of print, which is buffered:
raiserror ('My Print Statement', 10,1) with nowait

Your inserts may be uncommitted until the cursor loop has completed, in which case you could still see some data using the NOLOCK query hint.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1427353
Posted Wednesday, March 6, 2013 6:46 AM



Group: General Forum Members
Last Login: Yesterday @ 8:10 AM
Points: 13,970, Visits: 36,244
inside a cursor, that's normal; what you want to do instead of print is raise a low level error inside via RAISERROR the cursor so you can see things as it happens.

errors with NOWAIT are immediately visible in the results pane.

--print error immediately in batch
declare @i int,
@err varchar(100)
--set @i=1
while 0=0
SET @err = 'Progress So Far: Step ' + convert(varchar(30),ISNULL(@i,1)) + ' completed.'
raiserror (@err,0,1) with nowait
waitfor delay '00:00:02'
set @i=ISNULL(@i,1) + 1


help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1427358
Posted Wednesday, March 6, 2013 7:44 AM



Group: General Forum Members
Last Login: 2 days ago @ 2:28 PM
Points: 15,256, Visits: 15,229
Maybe an even better approach would be to remove the cursor and do your inserts set based? Cursors are notoriously slow and over used. They have their place but very rarely does doing some inserts like you are describing require a cursor.


Need help? Help us help you.

Read the article at for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1427411
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse