存储过程 事务

use stuDB
go
if exists(select * from sysobjects where name='bank')
	drop table bank
create table bank(
   customerName char(10),
   currentMoney money
)
go
alter table bank add
	constraint CK_currentMoney check(currentMoney>1)
insert into bank(customerName,currentMoney)
	select '张三',1000 union
	select '李四',10
select * from bank
go
use stuDB
go
set nocount on 
print '查看转账事务的余额'
select * from bank
go
begin tran
declare @sumError int
    set @sumError=0
	update bank set currentMoney=currentMoney-300 where customerName='张三'
		set @sumError=@sumError+@@ERROR 
	update bank set currentMoney=currentMoney-100 where customerName='李四'
		set @sumError=@sumError+@@ERROR
print '查看转账事务过程中的余额'
select * from bank

if @sumError>0
	begin
		print '交易失败,事务回滚'
		rollback tran
	end
else 
	begin
		print '交易成功,显示余额'
		commit tran
	end
go
print '查看交易后的余额'
select * from bank

 

posted @ 2012-04-26 09:43  杨伟明  阅读(149)  评论(0)    收藏  举报