SQL SERVER 游标+事务实例

use hbposev9_branch

begin

	declare autosale_cursor cursor for select distinct cVoucher_No from cf20190219

	open autosale_cursor

	declare @voucherno varchar(60)
	declare @i int,@error int

	set @i=1
    set @error=0

    begin tran  --申明事务

	fetch next from autosale_cursor into @voucherno

	while @@fetch_status=0
	begin
		print @voucherno
		print @i

		set @i=@i+1
        set @error= @error + @@ERROR   --0正确

		fetch next from autosale_cursor into @voucherno
	end

    if @error=0
		begin
			commit tran   --提交事务
		end
    else
		begin
			rollback tran --回滚事务
		end

	close autosale_cursor
	deallocate autosale_cursor

end
go

 

你可能感兴趣的:(sql,server)