日期:2014-05-18  浏览次数:20476 次

请教游标修改数据的问题
数据有
id     sale     quantity
三列组成
现在想把第三个数据的sale   值修改成888
declare   xg     scroll   cursor   for
select   id,sale,quantity   from   table2
open   xg
declare   @id   int,@sale   money,@quantity   int
fetch   absolute   3   from   xg   into   @id,@sale,@quantity
if   @@fetch_status=0
begin
set   rowcount   1
update   table2   set   sale=888   WHERE   id=@id   and   sale=@sale   and   quantity=@quantity
end

我认为这种方法不好,如果出现三项都完全重复的行就会出现错误修改
请问能不能像指针一样定位到哪个数据就修改哪个数据?多谢各位

------解决方案--------------------
如果表没有主键而且有可能存在三项数据都重复的情况,那就用临时表加一个主键,再来更新
------解决方案--------------------
请自己修改,我用了本机的表作测试
declare xg cursor scroll dynamic scroll_locks for
select id,name from tb
open xg
declare @id int
set @id=1
fetch next from xg
while @@fetch_status=0
begin
if @id=2
DELETE FROM tb WHERE CURRENT OF xg
set @id=@id+1
fetch next from xg
end
close xg
deallocate xg