sql 事物

    xiaoxiao2021-04-12  36

    ---开启事务 begin tran --错误扑捉机制,看好啦,这里也有的。并且可以嵌套。 begin try --语句正确 insert into lives(Eat, Play, Numb) values('猪肉','足球',1) --Numb为int类型,出错 insert into lives(Eat, Play, Numb) values('猪肉','足球','abc') --语句正确 insert into lives(Eat, Play, Numb) values('狗肉','篮球',2) end try begin catch select Error_number() as ErrorNumber, --错误代码 Error_severity() as ErrorSeverity, --错误严重级别,级别小于10 try catch 捕获不到 Error_state() as ErrorState , --错误状态码 Error_Procedure() as ErrorProcedure , --出现错误的存储过程或触发器的名称。 Error_line() as ErrorLine, --发生错误的行号 Error_message() as ErrorMessage --错误的具体信息 if(@@trancount>0) --全局变量@@trancount,事务开启此值+1,他用来判断是有开启事务 rollback tran ---由于出错,这里回滚到开始,第一条语句也没有插入成功。 end catch if(@@trancount>0) commit tran --如果成功Lives表中,将会有3条数据。
    转载请注明原文地址: https://ju.6miu.com/read-667235.html

    最新回复(0)