Text Resizer Text Resizer
July 30th, 2010
You're browsing: Scriptmatico.Com » SQL SERVER » Recorrer Una tabla Con T-SQL usando cursores

Recorrer Una tabla Con T-SQL usando cursores

Posted on Ago 13 in SQL SERVERby adminPrintText Resizer Text Resizer

Supongamos que necesitamos recorrer todos los registros de una tabla con T-SQL el lenguaje usado en los Procedimientos Almacenados de SQL Server, bien el codigo es muy sencillo y facil de entender.

Aqui les pongo el codigo:

SQL:
  1. //declaramos el Cursor para la consulta
  2. DECLARE TODOS CURSOR FOR
  3. SELECT PK_CVEPROFESOR FROM DBO.T012_CPROFESOR ORDER BY PK_CVEPROFESOR
  4. /*Abrimos el cursor*/
  5. OPEN TODOS
  6. /*Extraemos el Primer registo*/
  7. FETCH NEXT FROM TODOS
  8. INTO @PROFESOR
  9. /* imprimimos todos los registros mientras la variable @@FETCH_STATUS sea igual a 0*/
  10. WHILE @@FETCH_STATUS = 0
  11. BEGIN
  12. PRINT @PROFESOR
  13. /*Nos Movemos al siguiente registro*/
  14. FETCH NEXT FROM TODOS INTO @PROFESOR
  15. END
  16.  
  17. /*cerramos el cursor*/
  18. CLOSE TODOS
  19. DEALLOCATE TODOS

Espero les ayude...nos vemos en otra ocación

Post to Twitter


  • No Related Post

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Back to Top
[x] Cerrar
E-mail