事物的特点: 原子性 ,一致性, 隔离性, 持久性
事物的分类:显示事务 隐式事务 自动提交事务
T-sql 中使用事物的语句:
开始事务:begin transaction
提交事务:commit transaction
回滚事务:rollback transaction
事物的例子:
print '转账账号的余额'select* from AccountInfo declare @money money --声明一个变量来存放钱set @money =2000000 --转账的金额为200万declare @errorSum int --用于存放错误的个数set @errorSum =0--开始事务begin transaction update AccountInfo set currentMoney=currentMoney -@money where accountName='保利建设'; set @errorSum =@errorSum +@@ERROR update AccountInfo set currentMoney =currentMoney +@money where accountName ='莱钢集团' set @errorSum =@errorSum +@@ERROR if (@errorSum <>0)begin print '转账失败' rollback transaction endelsebegin print'转账成功' commit transactionend---------存储过程中使用事物--------------create proc proc_transfer @intoaccountName varchar(20), @uotaccountName varchar(20), @money money as if not exists (select * from AccountInfo where accountName=@intoaccountName) begin print '转账失败' return end if not exists (select * from AccountInfo where accountName=@uotaccountName) begin print '转账失败' return end declare @reeorsum int set @reeorsum =0 begin transaction update AccountInfo set currentMoney =currentMoney - @money where accountName =@uotaccountName set @reeorsum =@reeorsum +@@ERROR update AccountInfo set currentMoney =currentMoney +@money where accountName =@intoaccountName set @reeorsum =@reeorsum +@@ERROR if(@reeorsum <>0) begin print '转账失败' rollback transaction end else begin print'转账成功' commit transaction end------------------------------------------------