Cursore T-SQL

Cursore T-SQL



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)


Lo snippet seguente vuole essere un esempio di utilizzo di un Cursore T-SQL.

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 codice sorgente: Cursore_SQL.zip (354,00 bytes)

2 commenti

comments user
Marco

Ottimo, grazie!

    comments user
    Nicola Celiento

    Prego!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.