Home Forums SQL Server 2005 T-SQL (SS2K5) Looping through Cursor and executing Stored Procedure. Stored procedure runs only once for one record RE: Looping through Cursor and executing Stored Procedure. Stored procedure runs only once for one record

  • @lowell: I tend to disagree that design you posted will actually work as expected (beyond the typo FETCH NEXT FROM MynewCursor without moving INTO @item, @oper_num to the end, too):

    The first row is fetched and the processed outside the loop.

    Inside the loop this very same values are processed again.

    I'd rather use the following code:

    OPEN MynewCursor -- open the cursor

    FETCH NEXT FROM MynewCursor

    INTO @item, @oper_num

    --SELECT @item, @oper_num

    WHILE @@FETCH_STATUS = 0

    BEGIN

    --SELECT @item, @oper_num

    EXEC KI_Copy_QCItemTestSp 'MasterQualityItem', '10', @item, @oper_num, @Infobar OUTPUT

    --this is the part that was messed up: must be the last statemetn before the END/ section of WHILE loop

    -- including the variables

    FETCH NEXT FROM MynewCursor INTO @item, @oper_num

    END

    CLOSE MynewCursor

    DEALLOCATE MynewCursor

    Edit: as a side note: what's inside the the sproc KI_Copy_QCItemTestSp? Maybe the c.u.r.s.o.r. *cough* can be replaced completely...



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]