欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sqlserver 的一个有游标的过程

程序员文章站 2022-07-05 09:19:56
...
DEALLOCATE  c_emp //删除现有的同名游标

//声明一个游标 c_emp
declare c_emp  cursor for select NEWSTUDENT.IDENTY_NUM,NEWSTUDENT.EXAM_NUM,NEWSTUDENT.STUDENT_NUM from NEWSTUDENT,STUDENT where NEWSTUDENT.STUDENT_NUM=STUDENT.STUDENT_NUM

//声明三个变量
declare  @IDENTY_NUM nchar(20),@EXAM_NUM nchar(30),@STUDENT_NUM nchar(15)

//打开游标
 open c_emp
//取出数据
 fetch c_emp into @IDENTY_NUM,@EXAM_NUM,@STUDENT_NUM
//遍历
 while @@fetch_status=0
 Begin
//更新数据  
update STUDENT set IDENTY_NUM=@IDENTY_NUM,EXAM_NUM=@EXAM_NUM where student_num=@STUDENT_NUM
//取下一个数据 
 fetch c_emp into @IDENTY_NUM,@EXAM_NUM,@STUDENT_NUM
 End
close c_emp