begin try begin transaction insert into shiwu (asd) values ('aasdasda'); commit transaction end try begin catch select ERROR_NUMBER() as errornumber rollback transaction end catch
隐式设置事务
复制代码 代码如下:
set implicit_transactions on; -- 启动隐式事务 go begin try insert into shiwu (asd) values ('aasdasda'); insert into shiwu (asd) values ('aasdasda'); commit transaction; end try begin catch select ERROR_NUMBER() as errornumber rollback transaction; --回滚事务 end catch set implicit_transactions off; --关闭隐式事务 go
显示事务以下语句不能使用,隐式事务可以
复制代码 代码如下:
alter database; backup; create database; drop database; reconfigure; restore; update statistics;
显示事务可以嵌套使用
复制代码 代码如下:
--创建存储过程 create procedure qiantaoProc @asd nchar(10) as begin begin try begin transaction innerTrans save transaction savepoint --创建事务保存点 insert into shiwu (asd) values (@asd); commit transaction innerTrans end try begin catch rollback transaction savepoint --回滚到保存点 commit transaction innerTrans end catch end go begin transaction outrans exec qiantaoProc 'asdasd'; rollback transaction outrans