SQL函数:事务实例

事务就是负责把一系列操作看做一个独立的逻辑单元,这些操作要么同时成功,要么同时失败。下面是一个经典的例子:

create procedure TransferMoeny
(
    
@FromAccountNo varchar(50),-- 转出账号
    @ToAccountNo varchar(50),--转入账号
    @MoneyCount money--转账金额
)
as
--判断账号是否存在
if exists (select 1 from 帐户表 where 账号 = @FromAccountNo
begin
    
if exists (select 1 from 帐户表 where 账号 = @ToAccountNo
    
begin
        
--判断转出金额是否大于当前余额
        if (select 当前余额 from 帐户表 where 账号 = @FromAccountNo>= @MoneyCount
        
begin
            
--开始转账
            begin transaction
            
insert into [存取记录表] ([账号],[存取类型][存取金额]values(@FromAccountNo-1,@MoneyCount)
            
if @@error <> 0
            
begin
                
rollback transaction--发生错误则回滚事务,无条件退出l
                return
            
end
                       
insert into [存取记录表] ([账号],[存取类型][存取金额]values(@ToAccountNo1,@MoneyCount)
            
if @@error <> 0
            
begin
                
rollback tran
                
return
            
end
            
commit transaction --两条语句都完成,提交事务
        end
        
else    
            
raiserror ('转账金额不能大于该账号的余额',16,1)
    
end
    
else
    
raiserror ('转入账号不存在',16,1)

end
else
    
raiserror ('转出账号不存在',16,1)

Tag标签: SQL函数, 事务实例
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己备用的,对别人没有用 CREATE FUNCTION SplitStr (@splitString varchar(8000), @separate varchar(10)) RETURNS @returnTable table(id int, col_Value varchar(50)) AS BEGIN declare @thisSplitStr varchar(50) declare @thisSepIndex int declare @lastSepIndex int declare @i int set @lastSepIndex = 0 set @i = 1 if Right(@splitString ,len(@separate)) <> @separate set @splitString = @splitString + @separate set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex) while @lastSepIndex <= @thisSepIndex begin set @thisSplitStr = SubString(@splitString ,@lastSepIndex,@thisSepIndex-@lastSepIndex) set @lastSepIndex = @thisSepIndex + 1 set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex) insert into @returnTable values(@i, @thisSplitStr) set @i = @i + 1 end return END go --drop procedure sp_add_userFunction create procedure sp_add_userFunction @functionList varchar(5000), @userId varchar(50) as DECLARE @count INTEGER DECLARE @index INTEGER declare @functionId varchar(50) set @count = (select count(*) from SplitStr(@functionList,',')) set @index = 0 begin transaction delete from xt_user_function where user_id = @userId if @@error <> 0 begin rollback transaction--发生错误则回滚事务,无条件退出l return end while @index<@count begin set @functionId = (select col_Value from SplitStr(@functionList,',') where id = @index + 1) insert into xt_user_function(function_id, user_id) values (@functionId, @userId) SET @index=@index+1 end if @@error <> 0 begin rollback transaction--发生错误则回滚事务,无条件退出l return end commit transaction --两条语句都完成,提交事务 go

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值