SQL Cursor to display result in single transation

  • Hi Team,

    Here the SELECT query is fetchign the records corresponding to ITEM_DESCRIPTION in 5 separate transactions.

    How to change the cursor to display the 5 records in at a time in single transactions.

    CREATE TABLE #ITEMS (ITEM_ID uniqueidentifier NOT NULL, ITEM_DESCRIPTION VARCHAR(250) NOT NULL)INSERT INTO #ITEMSVALUES(NEWID(), 'This is a wonderful car'),(NEWID(), 'This is a fast bike'),(NEWID(), 'This is a expensive aeroplane'),(NEWID(), 'This is a cheap bicycle'),(NEWID(), 'This is a dream holiday')

    ---

    DECLARE @ITEM_ID uniqueidentifier

    DECLARE ITEM_CURSOR CURSOR

    FOR

    SELECT ITEM_IDFROM #ITEMS

    OPEN ITEM_CURSOR

    FETCH NEXT FROM ITEM_CURSOR INTO @ITEM_ID

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SELECT ITEM_DESCRIPTION

    FROM #ITEMSWHERE ITEM_ID = @ITEM_ID

    FETCH NEXT FROM ITEM_CURSOR INTO @ITEM_ID

    END

    CLOSE ITEM_CURSOR

    DEALLOCATE ITEM_CURSOR

  • From your example, you just need to get rid of the cursor. I'm sure that it would be more complicated than that, but the best option is if you share the real code to avoid the cursor completely.

    SELECT ITEM_DESCRIPTION

    FROM #ITEMS;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 2 posts - 1 through 1 (of 1 total)

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