日期:2014-05-17 浏览次数:20589 次
Update StudentMoney set Money = Money - @Money WHERE ID = @FROMID --从转出帐户中转出指定费用
------解决方案--------------------
create table StudentMoney
(ID int, MONEY int)
insert into StudentMoney
select 1, 4000 union all
select 2, 3000
CREATE PROCEDURE udt_MoveMoney
(@FromID int,@ToID int,@Money int)
AS
BEGIN TRANSACTION --开启事务
Update StudentMoney set Money = Money - @Money WHERE ID = @FROMID --从转出帐户中转出指定费用
IF @@error <> 0 or @@ROWCOUNT=0 --如果发生错误,回滚
BEGIN
ROLLBACK TRANSACTION
RETURN
END
Update StudentMoney set Money = Money + @Money WHERE ID = @ToID --转入帐户中增加指定费用
IF @@error <> 0 or @@ROWCOUNT=0 --如果发生错误,回滚
BEGIN
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION --如果没有错误,提交
GO
execute udt_MoveMoney 1,3,500
select * from StudentMoney
/*
ID MONEY
----------- -----------
1 4000
2 3000
(2 row(s) affected)
*/