Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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 06, 2013 6:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 6:11 AM
Points: 186, Visits: 474
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 06, 2013 6:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:08 AM
Points: 6,772, Visits: 12,875
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 06, 2013 6:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:53 AM
Points: 12,749, Visits: 31,108
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1427358
Posted Wednesday, March 06, 2013 7:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:57 PM
Points: 12,004, Visits: 11,035
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