jueves, 7 de junio de 2012

Cursor example in SQL

DECLARE @vNoSerie AS VARCHAR(15) DECLARE @vPrecio AS DECIMAL(10,2) DECLARE vCursor CURSOR FOR SELECT NoSerie, Precio FROM Ventas OPEN vCursor FETCH NEXT FROM vCursor INTO @vNoSerie, @vPrecio WHILE @@fetch_status = 0 BEGIN DECLARE @v VARCHAR(25) SET @v = (SELECT Estado FROM Movimientos WHERE NoSerie = @vNoSerie AND Importe = @vPrecio) --Visual Trace PRINT @vNoSerie + ' - ' + CONVERT(VARCHAR,@vPrecio) + ' - ' + @v IF (@v = 'DISPONIBLE') UPDATE Ventas SET Vendido = 1 WHERE NoSerie = @vNoSerie AND Precio = @vPrecio FETCH NEXT FROM vCursor INTO @vNoSerie, @vPrecio END CLOSE vCursor DEALLOCATE vCursor