sqlserver游标的简单使用

 时间:2026-02-15 19:38:24

1、第一步:在定义游标之前,需要先定义变量,本示例中只涉及三个需要用到的变量price,sums和name

在查询分析器中输入一行代码

declare @price float,@sums int,@name nvarchar(10)

sqlserver游标的简单使用

2、第二步:定义游标,输入以下代码

declare c_updateTotal cursor for

select price,sums,name from product

sqlserver游标的简单使用

3、第三步:打开游标并取一行数据,

open c_updateTotal

fetch next from c_updateTotal into @price,@sums,@name

sqlserver游标的简单使用

4、第四步:循环游标,来一行一行的处理我们的数据

while @@FETCH_STATUS=0

begin

--这里处理一些数据

fetch next from c_updateTotal into @price,@sums,@name

end

sqlserver游标的简单使用

5、第五步:在循环中间添加更新语句来批量对每一行进行修改

update product set total =@price*@sums where name=@name

sqlserver游标的简单使用

6、第六步:关闭游标与删除游标

close c_updateTotal 

deallocate  c_updateTotal

sqlserver游标的简单使用

7、第七步:完成,开始执行修改,以下为完整代码

declare @price float,@sums int,@name nvarchar(10)

declare c_updateTotal cursor for

select price , sums,name  from product

open c_updateTotal

fetch next from c_updateTotal into @price,@sums,@name

while @@FETCH_STATUS=0

begin

--这里处理一些数据

update product set total =@price*@sums where name=@name

fetch next from c_updateTotal into @price,@sums,@name

end

close c_updateTotal 

deallocate  c_updateTotal

  • word中给矩形标注添加紫色复合型轮廓样式
  • word样式怎么设置
  • word2019设置形状的轮廓
  • WORD文档偏右式分栏怎么设置
  • Word 如何修改样式
  • 热门搜索
    脚后跟开裂怎么办 骆驼祥子简介 郭冬临冯巩小品大全 禁片大全 怀孕一个月怎么避孕 qq头像大全 名车标志图片大全 炒芹菜怎么做好吃 大鳄龟怎么养 额头长痘怎么回事