zoukankan      html  css  js  c++  java
  • 存储过程与事务实现转账

    USE [master]
    GO
    /****** Object:  Database [BankSystem]    Script Date: 11/20/2013 16:12:45 ******/
    CREATE DATABASE [BankSystem] ON  PRIMARY 
    ( NAME = N'BankSystem', FILENAME = N'C:Program FilesMicrosoft SQL ServerMSSQL10_50.MSSQLSERVERMSSQLDATABankSystem.mdf' 
    
    , SIZE = 2304KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
     LOG ON 
    ( NAME = N'BankSystem_log', FILENAME = N'C:Program FilesMicrosoft SQL ServerMSSQL10_50.MSSQLSERVERMSSQLDATA
    
    BankSystem_log.LDF' , SIZE = 576KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
    GO
    ALTER DATABASE [BankSystem] SET COMPATIBILITY_LEVEL = 100
    GO
    IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
    begin
    EXEC [BankSystem].[dbo].[sp_fulltext_database] @action = 'enable'
    end
    GO
    EXEC sys.sp_db_vardecimal_storage_format N'BankSystem', N'ON'
    GO
    USE [BankSystem]
    GO
    /****** Object:  Table [dbo].[CardInfo]    Script Date: 11/20/2013 16:12:45 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[CardInfo](
    	[PKID] [int] IDENTITY(1,1) NOT NULL,
    	[CardNum] [nvarchar](19) NOT NULL,
    	[Balance] [money] NULL,
    PRIMARY KEY CLUSTERED 
    (
    	[PKID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) 
    
    ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Object:  Table [dbo].[Transfer_Event]    Script Date: 11/20/2013 16:12:45 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Transfer_Event](
    	[PKID] [int] IDENTITY(1,1) NOT NULL,
    	[FromCardID] [nvarchar](19) NOT NULL,
    	[ToCardID] [nvarchar](19) NOT NULL,
    	[TransMoney] [money] NULL,
    	[OccurTime] [datetime] NULL,
    PRIMARY KEY CLUSTERED 
    (
    	[PKID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) 
    
    ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    /****** Object:  StoredProcedure [dbo].[TranserMoney]    Script Date: 11/20/2013 16:12:46 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    --实现转账过程的存储过程实现如下(0代表成功,1代表转账源账户不存在,2代表转账目标账户不存在,3代表金额不足,4代表失败)
    CREATE procedure [dbo].[TranserMoney]
    (
    @fromCardNum varchar(19),        --转账源卡号
    @toCardNum  varchar(19),         --转账目标卡号 
    @tansMoney Money,                --转账金额
    @statue int  output              --0代表成功,1代表转账源账户不存在,2代表转账目标账户不存在,3代表金额不足,4代表失败
    )
    as
         declare @cardNum varchar(19)
         declare @balance Money=(select balance from CardInfo where  CardNum=@fromCardNum)
          --开始事务 
          if @balance is not null
          begin
           declare @exist int=(select COUNT(*) from CardInfo where  CardNum=@toCardNum)
           if @exist !=0
          begin
              if @balance>=@tansMoney
              begin
              
              begin transaction 
              declare @level int=0
                update CardInfo set Balance=Balance-@tansMoney where CardNum=@fromCardNum
                set @level=@level+@@ERROR
                update CardInfo set Balance=Balance+@tansMoney where CardNum=@toCardNum
                set @level=@level+@@ERROR
                 insert into Transfer_Event(FromCardID,ToCardID,TransMoney,OccurTime)values
    
    (@fromCardNum,@toCardNum,@tansMoney,GETDATE())
                set @level=@level+@@ERROR
                if @level=0
                begin
                   set @statue=0
                      commit transaction   --提交事务   
                end
                else
                begin
                set @statue=4
                  rollback transaction   --回滚事务          
                end
              
              end 
              else
              begin
              set @statue=3
              end
          end
           else
          begin
          set @statue=2
          end
          end
          else
          begin
          set @statue=1
          end
    GO
    /****** Object:  Default [DF__CardInfo__Balanc__014935CB]    Script Date: 11/20/2013 16:12:45 ******/
    ALTER TABLE [dbo].[CardInfo] ADD  DEFAULT ((0)) FOR [Balance]
    GO
    

     下面的测试代码

     --0代表成功,1代表转账源账户不存在,2代表转账目标账户不存在,3代表金额不足,4代表失败
    
    declare @statue int
     exec TranserMoney '101','103',100,@statue output
     select @statue
     
     select * from cardinfo
    select * from Transfer_Event
    

      

     

  • 相关阅读:
    存储过程 游标 有用
    Spring框架IOC容器和AOP解析 非常 有用
    业务逻辑;完成根据自动分单结果来决定进行的相关业务处理
    业务逻辑:完成基于分区关键字匹配的自动分单业务逻辑
    业务逻辑:完成基于CRM地址完全匹配的自动分单业务逻辑
    业务逻辑:完成客户下单后前台系统的数据处理并调用后台系统服务处理业务 webservice接口调用 有用
    Spring IOC 少
    Spring IOC 巨多 非常 有用
    Spring ioc
    LeetCode -- Remove Element
  • 原文地址:https://www.cnblogs.com/wth2010/p/3433829.html
Copyright © 2011-2022 走看看