全心思齐网

SQL游标的使用?

例子table1结构如下id intname varchar(50)declare @id intdeclare @name varchar(50)declare cursor1 cursor for --定义游标cursor1select * from table1 --使用游标的对象(跟据需要填入select文)open cursor1 --打开游标fetch next from cursor1 into @id,@name --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中while @@fetch_status=0 --判断是否成功获取数据beginupdate table1 set name=name+'1'where id=@id --进行相应处理(跟据需要填入SQL文)fetch next from cursor1 into @id,@name --将游标向下移1行endclose cursor1 --关闭游标deallocate cursor1

匿名回答于2024-05-24 02:52:40


相关知识问答