一 什么是事务的原子性?
执行多条SQL语句时(如执行多条更新语句),如果有一条SQL语句执行失败则会回滚数据到执行更新语句前
二 代码
1 执行脚本
begin try
begin transaction --开启事务
update userinfo set username = N'hc' where id =1 --更新用户名
update userinfo set id = N'xw' where id =1 --更新id ,由于id是整形,更新时赋值了字符型,所以该update语句会执行失败,所以会回滚
commit transaction --提交事务,把数据提交到数据库中
end try
begin catch --如果捕获到异常则会执行catch里的代码
rollback tran --回滚事务
end catch
转载请注明原文地址: https://ju.6miu.com/read-662212.html