SQL语句中事务与try catch使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句中事务与try catch使用相关的知识,希望对你有一定的参考价值。
普通SQL事务:
begin transaction tr
declare @error int;
set @error=0;
select * from Car_Brand
set @[email protected][email protected]@ERROR
select 1/0
set @[email protected][email protected]@ERROR
select * from AREA
set @[email protected][email protected]@ERROR
print ‘i have executed!‘
set @[email protected][email protected]@ERROR
print ‘@@error:‘+cast(@error as varchar(50))
if(@error<>0)
begin
print ‘executed failed‘;
rollback transaction tr
end
else
begin
print ‘executed success‘;
commit transaction tr
end
结合try catch 的SQL事务处理:
begin try
begin transaction tr
select * from Car_Brand
update car_log set [reason]=‘ceshiceshiceshi‘ where [modelid]=557;
select 1/0
print ‘i have executed!‘
select * from AREA
print ‘executed success‘;
commit transaction tr
end try
begin catch
print ‘executed failed‘;
rollback transaction tr
end catch
以上是关于SQL语句中事务与try catch使用的主要内容,如果未能解决你的问题,请参考以下文章
Sql语法高级应用之六:如何在Sql语句中如何使用TRY...CATCH