1. 程式人生 > >sql 事務處理 與 try catch

sql 事務處理 與 try catch

 

 1 begin transaction tr
 2    declare @error int;
 3    set @error=0;
 4    select * from Car_Brand
 5    set @error=@error+@@ERROR
 6    select 1/0
 7    set @error=@error+@@ERROR
 8    select * from AREA
 9    set @error=@error+@@ERROR
10    print 'i have executed!'
11    set @error
=@error+@@ERROR 12 13 print '@@error:'+cast(@error as varchar(50)) 14 if(@error<>0) 15 begin 16 print 'executed failed'; 17 rollback transaction tr 18 end 19 else 20 begin 21 print 'executed success'; 22 commit transaction tr 23 end
普通事務
 1 begin  try
 2 begin transaction tr
 3 
 4    select * from Car_Brand
 5    update car_log set [reason]='ceshiceshiceshi' where [modelid]=557;
 6    select 1/0
 7    print 'i have executed!'
 8    select * from AREA
 9    
10    
11    print 'executed success';
12    commit transaction tr
13 14 15 end try 16 begin catch 17 print 'executed failed'; 18 rollback transaction tr 19 end catch
事務結合 try catch