SQL Servercursors are generally used to traverse tables, andSQL Server can easily loop through cursors to implement SQL Server traversal records in tables. However, cursors are not recommended for actual development. We know that you can also implement SQL Server traversal tables with temporary tables or table variables, etc.The following example uses table variables to implement a simple loop:(run directly into Query Analyzer)
1. Declare @temp table
2. (
3. [id] int identity (, )
4. [name] varchar
5. )  
6. declare @tempId int, @tempName varchar (TEN)
7.
8. insert into @temp values (' a ')
9. insert into @temp values (' B ')
10. insert into @temp values (' C ')
11. insert into @temp values (' d ')
. INSERT into @temp values (' E ')
.
. --select * from @temp
A .
16. while exists (select [id] from @temp)
17. begin
. SET ROWCOUNT 1
. SELECT @Tempid= [id],@Tempname=[name] from @temp
20. set rowcount 0
. Delete from @temp where [id] = @tempId
.
23. print ' Name:----' [email protected]
24. end