SQL数据库高级 事务及储存过程写(转钱代码)

create database wzx
create table biao(id int primary key identity(1,1),
name nvarchar(100),
money int
)
insert biao values
('王祖顼',500),
('白浩杰',500)
select * from biao
create proc proc_zhuan(@fromname nvarchar(100),@toname nvarchar(100),@money int)
as
begin tran
declare @cuo int=0
declare @temp int = ( select money from biao where name=@fromname)
if ((@temp-@money)<0)
begin 
set @cuo+=1
end

update biao set money=@temp-@money where name=@fromname
set @cuo+=@@error
update biao set money=( select money from biao where name=@toname)+@money where name=@toname
set @cuo+=@@error

if @cuo>0
begin
rollback tran
end
else
begin
commit tran
end

exec  proc_zhuan '王祖顼','白浩杰',100
select * from biao

你可能感兴趣的:(数据库高级,数据库,sql,oracle)