enero 04, 2011

Cursores FAST-FORWARD en SQL Server

If you have no choice but to use a server-side cursor in your application, try to use a FORWARD-ONLY or FAST-FORWARD, READ-ONLY cursor. When working with unidirectional, read-only data, use the FAST_FORWARD option instead of the FORWARD_ONLY option, as it has some internal performance optimizations to speed performance. This type of cursor produces the least amount of overhead on SQL Server.

http://www.sql-server-performance.com/tips/cursors_p1.aspx


EJEMPLO de uso:
--Declaramos las variables
Declare @cod as int
Declare CURSOR1 cursor FAST_FORWARD for
select Valor from tabla1
Open CURSOR1

-- Avanzamos un registro y cargamos en las variables los valores encontrados en el primer registro
fetch next from CURSOR1
into @cod
while @@fetch_status = 0
Begin
--intruccion: la necesidad de implementar el cursor.
print convert(varchar(10), @cod)

-- Avanzamos otro registro
fetch next from CURSOR1
into @cod
End

-- cerramos el cursor
close CURSOR1
deallocate CURSOR1

No hay comentarios.: