Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 16, 2015 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/c5f05138aa7dc5104ab5 to your computer and use it in GitHub Desktop.
Save Fhernd/c5f05138aa7dc5104ab5 to your computer and use it in GitHub Desktop.
Demostración de creación y uso de cursores en T-SQL.
-- OrtizOL - xCSw - http://ortizol.blogspot.com
-- Deshabilita mostrar contador de filas en los resultados:
SET NOCOUNT ON;
-- 0. Declaración de variables auxiliares:
DECLARE @IDTienda as INT;
DECLARE @NombreTienda as NVARCHAR(50);
-- 1. Declaración de variable CURSOR:
DECLARE @cursor_tienda as CURSOR;
SET @cursor_tienda = CURSOR FOR
SELECT BusinessEntityID, Name
FROM Sales.Store;
-- 2. Apertura del cursor
OPEN @cursor_tienda;
-- 3. Recuperación de registros uno a la vez:
FETCH NEXT FROM @cursor_tienda INTO @IDTienda, @NombreTienda;
-- 4. Ciclo para iterar cada registro:
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'ID Tienda: ' + cast(@IDTienda as VARCHAR (50)) + ' - Nombre Tienda: ' + @NombreTienda;
FETCH NEXT FROM @cursor_tienda INTO @IDTienda, @NombreTienda;
END
-- 5. Cierre del cursor:
CLOSE @cursor_tienda;
-- 6. Liberación de recursos del cursor:
DEALLOCATE @cursor_tienda;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment