Cursor.. cmds don’t seem to update until everything has finished

  • 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

  • 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

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

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply