Transact-SQL: utilizzo del CURSOR

Una cosa molto banale, dichiarare ed usare un cursore in una store procedure in Transact-SQL:

Supponiamo di avere una tabella con tre campi: (ID, CAMPO1, CAMPO2)

DECLARE @ID INTEGER
DECLARE @CAMPO1 VARCHAR(10)
DECLARE @CAMPO2 VARCHAR(10)

DECLARE c_curs CURSOR FOR

select * from tabella

OPEN c_curs

FETCH NEXT FROM c_curs
INTO @ID, @CAMPO1, @CAMPO2

WHILE @@FETCH_STATUS = 0

BEGIN

-- Faccio qualche cosa con i record della tabella

-- Passo al record successivo
FETCH NEXT FROM c_curs
INTO @ID, @CAMPO1, @CAMPO2

END

CLOSE c_curs
DEALLOCATE c_curs

La documentazione di mamma Microsoft: http://msdn.microsoft.com/it-it/library/ms180169(SQL.90).aspx

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>