zoukankan      html  css  js  c++  java
  • sql2005事务的使用

    公司里的每个项目都有很多一次要操作两次以上数据库的,为了数据的完整性,这就需要使用事务

    事务也属于存储过程,代码如下

    ALTER proc [dbo].[js_takemoney]
    @userid bigint,                            --参数
    @usercode varchar(50),
    @amount numeric(18,2),
    @rmb numeric(18,2)
    as
    begin
    declare @balance numeric(18,2)
    declare @takecharg numeric(18,2)
    declare @takechargmoney numeric(18,2)
    declare @turetype numeric(18,2)
    declare @getamount numeric(18,2) --实际金额
    declare @waterid int --流水账表的ID
    select @takecharg =TakeMoneyChargRate ,@turetype = UStoRMB from tb_GlobeParam where id = '1'
    set @takechargmoney = @amount * @takecharg/100
    set @getamount =@amount - (@amount * @takecharg/100) --手续费的计算


    begin tran takemoney         --开始事务
    update tb_user set BonusAccount = BonusAccount - @amount where userid = @userid --更新用户表
    select @balance = BonusAccount from tb_user where userid = @userid
    insert into tb_AccountDetails values (@userid,@usercode,'1','提现','0',@amount,@balance,getdate()) --流水账表记录插入
    select top 1 @waterid = @@IDENTITY from tb_AccountDetails where userid = @userid
    insert into tb_TakeMoney values (getdate(),@amount,@getamount,@rmb,@takechargmoney,@userid,'0',@waterid,getdate()) --提现表记录插入
    if @@error<>0             --如果失败
    begin
    rollback tran takemoney         -- 回滚事务
    return
    end
    commit tran mytran           --提交事务
    end

    这写法是网上一位不知名的网友提供的,我忘了……这个例子是我项目里的例子

    希望对初学者有所帮助

  • 相关阅读:
    发邮件(asp.net2.0)(转)
    教师节祝福短信
    量子学习及思考13人机交互很快将面临交互模式的进化2 人工智能
    MongoDB(1) 简单配置
    CreateCompatibleDC
    设置环境变量的作用
    vs2008中调用matlab生成的dll
    resolve the maado15.dll
    错误3:系统找不到指定的路径
    C++关键字volatile
  • 原文地址:https://www.cnblogs.com/CommonDream/p/2321223.html
Copyright © 2011-2022 走看看