sql server 事务的例子

begin tran ---开启事务

begin try  

		/*可以写游标,但是要注释掉begin 和end*/
		--begin

		--end
		--go

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  --提交

猜你喜欢

转载自blog.csdn.net/gdali/article/details/86502895