SQL设计一个事务,给所有员工增加工资50,要求5人作为一个事务提交一次。

2025年03月23日 21:27
有1个网友回答
网友(1):

declare o_cursor cursor for select * from employee
declare @eid int,@cnt int
set @cnt = 0
open o_cursor

fetch next from o_cursor into @eid
while @@fetch_status = 0
begin
set @cnt = @cnt + 1
update employee set salary=salary+50 where eid=@eid

if ( @@error <> 0 ) rollback tran
if ( @cnt = 5 )
begin
commit
set @cnt=0
end
fetch next from o_cursor into @eid --这句一定要写,否则麻烦大了. :) 死循环
end

commit

close o_cursor
deallocate o_cursor