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
Author
Message
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: Wednesday, July 13, 2016 4:49 AM
Points: 202, Visits: 522
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


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, December 1, 2016 10:11 AM
Points: 8,587, Visits: 18,753
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


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:48 AM
Points: 14,541, Visits: 38,381
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.
example:

--print error immediately in batch
declare @i int,
@err varchar(100)
--set @i=1
while 0=0
begin
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
end



Lowell

--
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


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: 2 days ago @ 2:44 PM
Points: 16,134, Visits: 16,835
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 http://www.sqlservercentral.com/articles/Best+Practices/61537/ 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