Posted by: ayhan | 29/11/2013

SQL Server’da CURSOR Kullanımı

Alt alta olan tablo satırlarını bir stringte birleştirmek için aşağıdaki şekilde kullandım;

DECLARE @PRODUCTTYPEIDS NVARCHAR(3000)
DECLARE MY_Cursor CURSOR FOR
SELECT ProductTypeID FROM #TEMP_PRODUCTS
DECLARE @ProductTypeID INT
OPEN MY_Cursor;
FETCH NEXT FROM MY_Cursor INTO @ProductTypeID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @PRODUCTTYPEIDS = ISNULL(@PRODUCTTYPEIDS,”)+ ‘,’ + CONVERT(NVARCHAR(10),@ProductTypeID)
FETCH NEXT FROM MY_Cursor INTO @ProductTypeID
END;
CLOSE MY_Cursor;
DEALLOCATE MY_Cursor;
SELECT @PRODUCTTYPEIDS
GO

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: