Created
July 16, 2015 01:24
-
-
Save Fhernd/c5f05138aa7dc5104ab5 to your computer and use it in GitHub Desktop.
Demostración de creación y uso de cursores en T-SQL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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