Text Resizer Text Resizer
February 5th, 2012
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 PrintText 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

Related posts:

  1. Imprimir una tabla completa con PHP y MYSQL

Leave a Reply

You must be logged in to post a comment.

Back to Top