IF WHERE语句 事件transaction
--循环练习,1到100偶数之和
declare @num int;
declare @sum int;
set @sum=0;
set @num=1;
while @num<=100
begin
if @num%2=0
begin
set @sum=@sum+@num
end
set @num=@num+1;
end
print @sum
--事物 transaction练习
--利用@@error计数
create table BankList (
Id int identity(1,1) primary key,
Account varchar(5) not null,
Balance decimal(18,2) not null
)
alter table banklist add constraint DF_BankList_Balance default 0 for Balance
alter table banklist add constraint CK_BankList_Balande check(balance>=0)
--利用begin try end try,begin catch end catch
begin transaction tran1
declare @error int ;
set @error=0;
update banklist set Balance=Balance-1500.00 where Account ='1002';
set @error=@error+@@error;
update banklist set balance=balance+1500.00 where Account='1001'
set @error=@error+@@error;
if @error=0
begin
commit transaction
print '提交了转账'
end
else
begin
rollback transaction
print '未提交'
end

浙公网安备 33010602011771号