软件开发初学者对存储过程和事务都不是很理解,今天先举个事务的简单例子大家可以先理解下,随后会把存储过程给大家一块讲解。
开始事务:begin transaction
提交事务:commit transaction
回滚事务:rollback transaction
例子:
begin transaction
declare @errorSum int --定义局部变量
set @errorSum=0 --初始化临时变量
update bank set currentMoney= currentMoney-1000 where customerName='张三'
set @errorSum=@errorSum+@@error --累计是否有错误
update bank set currentMoney= currentMoney+1000 where customerName='李四'
set @errorSum=@errorSum+@@error --累计是否有错误
if @errorSum<>0 --如果有错误
begin
rollback transaction
end
else
begin
commit transaction
end
go
开始事务:begin transaction
提交事务:commit transaction
回滚事务:rollback transaction
例子:
begin transaction
declare @errorSum int --定义局部变量
set @errorSum=0 --初始化临时变量
update bank set currentMoney= currentMoney-1000 where customerName='张三'
set @errorSum=@errorSum+@@error --累计是否有错误
update bank set currentMoney= currentMoney+1000 where customerName='李四'
set @errorSum=@errorSum+@@error --累计是否有错误
if @errorSum<>0 --如果有错误
begin
rollback transaction
end
else
begin
commit transaction
end
go