在SQL Server 中,可以使用游标对需要查询的表单进行遍历,这里列举一个简单的例子
首先创建需要遍历的表
create table student
(
StudentName varchar(20),
StudentNo varchar(10)
)
在表中预插入数据(略)
使用游标进行遍历
declare @TmpName varchar(20),
@TmpNo varchar(10)
declare cursor_test cursor for
select StudentName,StudentNo
from student
open cursor_test --打开游标
fetch next from cursor_test into @TmpName,@TmpNo
while @@fetch_status=0
begin
--数据相关处理
fetch next from cursor_test into @TmpName,TmpNo
end
close cursor_test --关闭游标
deallocate cursor_test --释放游标