TOPIC

磷光与烟火逆流

一个敲代码的程序猿

SQL Server CURSOR 游标

使用CURSOR 实现FOR循环

--声明变量
DECLARE @field1  varchar(100),
    @field2   varchar(100),
    @field3  varchar(100)

declare cur CURSOR FOR select field1,field2,field3 from tablename --创建游标
OPEN cur
FETCH NEXT FROM cur INTO  @field1,@field2,@field3 
while (@@fetch_status = 0) --如果查询数据为空 则@@fetch_status=-1 else @@fetch_status=0
begin
	update tablename2 set  @field1 = @field2 + @field3

	FETCH NEXT FROM cur INTO @field1,@field2,@field3
end  
CLOSE cur--关闭游标  
DEALLOCATE cur--释放游标
posted @ 2021-05-20 16:23  Jacobyang  阅读(71)  评论(0编辑  收藏  举报