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--释放游标