May 25, 2015 at 7:51 am
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
May 25, 2015 at 10:24 am
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;
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply