create table yxd_student(id int,name nvarchar(10),age int) insert into yxd_student select 1,'张三',25 insert into yxd_student select 2,'李四',30 go if CURSOR_STATUS('global','cur') <> -3 --状态-3表示游标不存在 begin close cur deallocate cur end declare cur cursor scroll dynamic -- 得声明为动态游标 for select id,name from yxd_student FOR UPDATEgo declare @id int,@name char(20),@temp char(20) open cur fetch first from cur into @id,@name while @@FETCH_STATUS=0 begin if @name='李四' begin print '更新中....' update yxd_student set age=age+1 where CURRENT OF cur; print '更新完毕~' end else begin print '继续找' end fetch next from cur into @id,@name end close cur deallocate cur go select * from yxd_student drop table yxd_student /* id name age ----------- ---------- ----------- 1 张三 25 2 李四 31(2 行受影响)*/
insert into yxd_student select 1,'张三',25
insert into yxd_student select 2,'李四',30
go
if CURSOR_STATUS('global','cur') <> -3 --状态-3表示游标不存在
begin
close cur
deallocate cur
end
declare cur cursor scroll dynamic
-- 得声明为动态游标
for select id,name from yxd_student
FOR UPDATEgo
declare @id int,@name char(20),@temp char(20)
open cur
fetch first from cur into @id,@name
while @@FETCH_STATUS=0
begin if @name='李四'
begin
print '更新中....'
update yxd_student set age=age+1 where CURRENT OF cur;
print '更新完毕~'
end
else
begin
print '继续找'
end
fetch next from cur into @id,@name
end
close cur
deallocate cur
go
select * from yxd_student
drop table yxd_student
/*
id name age
----------- ---------- -----------
1 张三 25
2 李四 31(2 行受影响)*/
这次真的被自己搞死了,原来昨天晚上做实验的时候在这张表示建了个触发器,替换了原来的update操作,难怪怎么更新都不行,悲剧啊!!!!!