begin tran tran_AddUserInfo --开始事务
declare @tran_error int;
set @tran_error=0;
begin try
insert into dbo.UserInfo values(2016009,'aaa','2016-08-19 09:13:41.227','男')
insert into dbo.UserInfo values(2016009,'bbb','2016-08-19 09:13:41.227','哼哼哼')
insert into dbo.UserInfo values(2016009,'ccc','2016-08-19 09:13:41.227','哈哈哈')
end try
begin catch
set @tran_error=@tran_error+1; --加分号或不加都能正常执行
end catch
if(@tran_error>0)
begin
rollback tran tran_AddUserInfo; --执行出错,回滚事务(指定事务名称)
print @tran_error;
end
else
begin
commit tran tran_AddUserInfo; --没有异常,提交事务(指定事务名称)
print @tran_error;
end
事务代码2:
begin tran tran_AddUserInfo --开始事务
declare @tran_error int;
set @tran_error=0;
begin try
insert into dbo.UserInfo values(2016009,'aaa','2016-08-19 09:13:41.227','男')
insert into dbo.UserInfo values(2016009,'bbb','2016-08-19 09:13:41.227','哈哈')
insert into dbo.UserInfo values(2016009,'ccc','2016-08-19 09:13:41.227','哈哈')
end try
begin catch
set @tran_error=@tran_error+1; --加分号或不加都能正常执行
end catch
if(@tran_error>0)
begin
rollback tran; --执行出错,回滚事务(不指定事务名称)
print @tran_error;
end
else
begin
commit tran; --没有异常,提交事务(不指定事务名称)
print @tran_error;
end