post icon

T-SQL Cursor

The following snippet is an example of using T-SQL Cursor.

USE Northwind
GO 

DECLARE @CATEGORIA  NVARCHAR(15)
 
DECLARE MY_CURSOR CURSOR FOR 
      SELECT CategoryName 
		FROM Categories ORDER BY CategoryName 

OPEN MY_CURSOR 

FETCH NEXT FROM MY_CURSOR 
INTO @CATEGORIA 

WHILE @@FETCH_STATUS = 0
 BEGIN
    
  --DO JOB INTO WHILE
  PRINT @CATEGORIA 

    FETCH NEXT FROM MY_CURSOR 
    INTO @CATEGORIA
 END

CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR

Download source code: Cursore_SQL.zip (354,00 bytes)

February 18th, 2009 @ 07:00

2 Comments

Leave a comment
  1. Marco
    February 2nd, 2017 at 12:20 #

    Ottimo, grazie!

Leave a Reply

Are you human? Time limit is exhausted. Please reload CAPTCHA.