sql server 事务的例子

begin tran ---开启事务

declare @trancount int;
set @trancount=0;

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  --错误的具体信息
          set @trancount = @trancount +1;
end catch

if(@trancount>0)
    begin
        rollback tran  ---由于出错,这里回滚到开始,第一条语句也没有插入成功。
    end
else
    begin
        commit tran  --提交
    end


 

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