t-sql事务代码问题 悬赏分:0 - 解决时间:2007-8-7 22:16 create database bankinfo gouse bankinfo goif exists(select * from sysobjects where name='bank') drop table bank create table bank ( customerName varchar (10), currentMoney money ) goalter table bank add constraint ck_currentMoney check (currentMoney>=1) goinsert into bank values ('张三',1000) insert into bank values ('李四',1)select * from bankbegin transaction declare @errorSum int set @errorSum=0 update bank set currentMoney=currentMoney-1000 where customerName='张三' update bank set currentMoney=currentMoney+1000 where customerName='李四' set @errorSum=@errorSum+@@error select 错误=@errorSum ----------------------------这为什么显示错误是0print '查看转帐事务过程中的余额' select * from bankif(@errorSum<>0) begin print '交易失败,回滚事务' rollback transaction end else begin print'交易成功,提交事务,写入硬盘,永久保存' commit transaction endprint'查看转帐后余额'
start transaction; select moneyfrom account where username='caiwm' for update; update account set money= money+100 where username='caiwm' ; update maxaccount set money= money+100 where username='caiwm' ; update minaccount set money= money-100 where username='caiwm'; COMMIT; Rollback; 我建了这个 该怎么查看
start transaction;
insert into tb select ...
commit;
--或者 rollback;
悬赏分:0 - 解决时间:2007-8-7 22:16
create database bankinfo
gouse bankinfo
goif exists(select * from sysobjects where name='bank')
drop table bank
create table bank
(
customerName varchar (10),
currentMoney money
)
goalter table bank
add constraint ck_currentMoney check (currentMoney>=1)
goinsert into bank values ('张三',1000)
insert into bank values ('李四',1)select * from bankbegin transaction
declare @errorSum int
set @errorSum=0
update bank set currentMoney=currentMoney-1000
where customerName='张三'
update bank set currentMoney=currentMoney+1000
where customerName='李四'
set @errorSum=@errorSum+@@error
select 错误=@errorSum ----------------------------这为什么显示错误是0print '查看转帐事务过程中的余额'
select * from bankif(@errorSum<>0)
begin
print '交易失败,回滚事务'
rollback transaction
end
else
begin
print'交易成功,提交事务,写入硬盘,永久保存'
commit transaction
endprint'查看转帐后余额'
select moneyfrom account where username='caiwm' for update;
update account set money= money+100 where username='caiwm' ;
update maxaccount set money= money+100 where username='caiwm' ;
update minaccount set money= money-100 where username='caiwm';
COMMIT;
Rollback;
我建了这个 该怎么查看
CREATE TABLE t(idd INT );start TRANSACTION;
INSERT t SELECT 1;
COMMIT;
+------+
| idd |
+------+
| 1 |
+------+--说明成功了~
start TRANSACTION;
INSERT t SELECT 2;
ROLLBACK; +------+
| idd |
+------+
| 1 |
+------+--t表里面还是只有一个1 说明2没被插入到表t里面 说明事务回滚