use master
go
if exists(select * from sysDatabases where name = 'BankDB')
drop database BankDB
go
create database BankDB
go
use BankDB
go
--建用户信息表
if exists(select * from sysObjects where name = 'Xxl_UserInfo')
drop table Xxl_UserInfo
go
create table Xxl_UserInfo
(
    Xxl_User_Id            int                not null    primary key identity ,
    Xxl_User_Name        )    not null    ,
    Xxl_User_Sex        bit                not null    ,
    Xxl_User_IDcard        )        not null    unique ,
    Xxl_User_Moblie        )        not null    check(Xxl_User_Moblie like '1[3579][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
    Xxl_User_Address    )    not null
)
go
--建用户卡信息表
if exists(select * from sysObjects where name = 'Xxl_CardInfo')
drop table Xxl_CardInfo
go
create table Xxl_CardInfo
(
    Xxl_Card_No            )        not null    primary key check(Xxl_Card_No like '66668888[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') ,
    Xxl_Card_pwd        )            ') ,
    From_Xxl_User_Id    int                not null    references Xxl_UserInfo(Xxl_User_Id),
    Xxl_Card_Date        DateTime        not null    default(getdate()) ,
    Xxl_Card_Balance    ,)    ) ,
    Xxl_Card_State        ,,)),
    Xxl_Card_Text        )    not null
)
go
--建交易信息表
if exists(select * from sysObjects where name = 'Xxl_TransInfo')
drop table Xxl_TransInfo
go
create table Xxl_TransInfo
(
    Xxl_Trans_FlowNum        int                not null    identity primary key    ,
    From_Xxl_Card_No        )        not null    references Xxl_CardInfo(Xxl_Card_No) ,
    Xxl_Trans_Type            ,)) ,
    Xxl_Trans_Quota            ,)    ) ,
    Xxl_Trans_Date            DateTime        not null    default(getdate()) ,
    Xxl_Trans_ed_Balance    ,)    ) ,
    Xxl_Trans_Text            )        not null
)
go
------添加用户信息
,','湖北武汉')
,','湖北武汉')
,','湖北武汉')
----添加用户卡信息
,,,'使用')
,,,'使用')
,,,'使用')
,,,'使用')
----添加交易信息
,,,'存入300元')
,,,'存入300元')
,,,'存入6000元')
,,,'转账3000元给6666888812454852')
,,,'6666888865896548转入的3000元')
,,,'存入3300元')
,,,'存入3000元')
,,,'取出2500元')
------备份交易信息表
select * into Xxl_TransInfo_BAK from Xxl_TransInfo
--------查询各表数据
--select * from Xxl_UserInfo
--select * from Xxl_CardInfo
--select * from Xxl_TransInfo
--select * from Xxl_TransInfo_BAK
----------------------------------------创建函数----------------------------------------
--加逗号的函数
if exists(select * from sysObjects where name='function_JiaDouhao')
    drop function function_JiaDouhao
go
,))
    ) as
    begin
        ))
        ))
        )
            begin
                )+@b
                )
            end
        return @a+@b
    end
go
------------------------------------------结束------------------------------------------
----------------------------------------创建视图----------------------------------------
--用户信息视图
if exists(select * from sysObjects where name    ='vw_UserInfo')
    drop view vw_UserInfo
go
create view vw_UserInfo
    as
    select
        Xxl_User_Id                编号,
        Xxl_User_Name            姓名,
        case Xxl_User_Sex
             then '女'
             then '男'
            end                    性别,
        Xxl_User_IDcard            身份证,
        Xxl_User_Moblie            联系电话,
        Xxl_User_Address        籍贯
        from Xxl_UserInfo
go
--使用视图
--select * from vw_UserInfo
--卡信息视图
if exists(select * from sysObjects where name='vw_CardInfo')
    drop view vw_CardInfo
go
create view vw_CardInfo
    as
    select
        Xxl_Card_No                                    卡号,
        Xxl_User_Name                                姓名,
        Xxl_Card_Balance                            余额,
        Xxl_Card_Date                                开卡日期,
        case Xxl_Card_State
             then '正常'
             then '冻结'
             then '注销'
        end                                            状态,
        dbo.function_JiaDouhao(Xxl_Card_Balance)    货币表示
        from Xxl_UserInfo UserInfo inner join Xxl_CardInfo CardInfo on UserInfo.Xxl_User_Id = CardInfo.From_Xxl_User_Id
go
--使用视图
--select * from vw_CardInfo
--交易记录视图
if exists(select * from sysObjects where name='vw_TransInfo')
    drop view vw_TransInfo
go
create view vw_TransInfo
    as
    select    ----卡号,交易日期,交易类型,交易金额,余额,描述
        Xxl_Card_No                卡号,
        Xxl_Trans_Date            交易日期,
        case Xxl_Trans_Type
             then '存入'
             then    '支取'
        end                     交易类型,
        case Xxl_Trans_Type
             ),Xxl_Trans_Quota)
             ),Xxl_Trans_Quota)
            end                    交易金额,
        Xxl_Trans_ed_Balance    余额,
        Xxl_Trans_Text            描述
        from Xxl_CardInfo CardInfo inner join Xxl_TransInfo TransInfo on CardInfo.Xxl_Card_No = TransInfo.From_Xxl_Card_No
go
--使用视图
--select * from vw_TransInfo
--------------------------------------------结束--------------------------------------------
----------------------------------------创建存储过程----------------------------------------
--1、    查询余额
if exists(select * from sysObjects where name='p_SelectBalance')
    drop proc p_SelectBalance
go
create proc p_SelectBalance
    )
as
    select 货币表示 as 余额 from vw_CardInfo where 卡号 = @CardNo
go
--exec p_SelectBalance '6666888845125214'
--2、    查询某两日期之间交易记录
if exists(select * from sysObjects where name='p_SelectStart_StopDate')
    drop proc p_SelectStart_StopDate
go
create proc p_SelectStart_StopDate
    ),
    @StartDate datetime,
    @StopDate datetime
as
    ,@StopDate)
go
--exec p_SelectStart_StopDate '6666888845125214','1990-1-1','2018-9-9'

--3、    修改密码功能
if exists(select * from sysObjects where name='p_Update_Pwd')
    drop proc p_Update_Pwd
go
create proc p_Update_Pwd
    ),
    ),
    )
as
    update Xxl_CardInfo set Xxl_Card_pwd=@CardPwdStop where Xxl_Card_No = @CardNo and Xxl_Card_pwd = @CardPwdStart
go
--exec p_Update_Pwd '6666888845125214','666888','548888'
--4、    存款功能(备份)
if exists(select * from sysObjects where name='p_SeveMoney')
    drop proc p_SeveMoney
go
create proc p_SeveMoney
    ),
    ,),
    ) output
as
    --判断存款金额

    begin
        set @errMeg =  '输入金额有误!'

    end
    begin tran

    ,)
    select @startBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @CardNo
    --添加存款记录
    ,), @Quota) + '元'))
    select @err = @@ERROR + @err
    --更新余额
    update Xxl_CardInfo set Xxl_Card_Balance = (@startBalance + @Quota) where Xxl_Card_No = @CardNo
    select @err = @@ERROR + @err

    begin
        set @errMeg =  '操作成功'
        commit tran

    end
    begin
        set @errMeg =  '未知错误!'
        rollback tran

    end
go
--5、    取款功能(备份)
if exists(select * from sysObjects where name='p_GetMoney')
    drop proc p_GetMoney
go
create proc p_GetMoney
    ),
    ,),
    ) output
as
    --判断取款金额

    begin
        set @errMeg = '输入金额有误!'

    end
    --查询原有余额
    ,)
    select @startBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @CardNo
    --判断余额是否足够
    if @startBalance > @Quota
    begin
        set @errMeg = '余额不足!'

    end
    begin tran

    --添加取款记录
    ,), @Quota) + '元'))
    select @err = @@ERROR + @err
    --更新余额
    update Xxl_CardInfo set Xxl_Card_Balance = (@startBalance - @Quota) where Xxl_Card_No = @CardNo
    select @err = @@ERROR + @err

    begin
        set @errMeg = '操作成功'
        commit tran

    end
    else
    begin
        set @errMeg = '未知错误!'
        rollback tran

    end
go
--6、    转帐功能(备份)
if exists(select * from sysObjects where name='p_TeansferMoney')
    drop proc p_TeansferMoney
go
create proc p_TeansferMoney
    ),
    ),
    ,),
    ) output
as
    --判断目标账户是否为本身
    if @FromCardNo != @ToCardNo
    begin
        set @errMeg =  '目标账户不可以为自己!'

    end
    --判断目标账户是否存在
    if not exists(select * from Xxl_CardInfo where Xxl_Card_No = @ToCardNo)
    begin
        set @errMeg =  '目标账户不存在!'

    end
    --判断转账金额是否正确

    begin
        set @errMeg =  '输入金额有误!'

    end
    --查询From原有余额
    ,)
    select @FromStartBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @FromCardNo
    --判断From余额是否充足
    if @FromStartBalance < @Quota
    begin
        set @errMeg =  '余额不足!'

    end
    begin tran

    --查询To原有余额
    ,)
    select @ToStartBalance=Xxl_Card_Balance from Xxl_CardInfo where Xxl_Card_No = @ToCardNo
    --更新余额
    update Xxl_CardInfo set Xxl_Card_Balance = (@FromStartBalance - @Quota) where Xxl_Card_No = @FromCardNo
    select @err = @@ERROR + @err
    update Xxl_CardInfo set Xxl_Card_Balance = (@ToStartBalance + @Quota) where Xxl_Card_No = @ToCardNo
    select @err = @@ERROR + @err
    --添加交易记录
    ,), @Quota) + '元给'+@ToCardNo)
    select @err = @@ERROR + @err
    ,), @Quota) + '元'))
    select @err = @@ERROR + @err

    begin
        set @errMeg =  '操作成功!'
        commit tran

    end
    else
    begin
        set @errMeg =  '未知错误!'
        rollback tran

    end
go
--exec p_TeansferMoney '6666888812454852','6666888845125214',300.00
--7、    随机产生卡号(卡号格式为:8228 6688 XXXX XXXX) 注:随机产生的卡号已经存在的不能用
if exists(select * from sysObjects where name='P_GenerateBankcard')
    drop proc P_GenerateBankcard
go
create proc P_GenerateBankcard
    ) output
as 

    begin
        ), ,),)
        if not exists(select * from Xxl_CardInfo where Xxl_Card_No = @Card)
            break
    end
go
--declare @Card char(16)
--exec P_GenerateBankcard @Card output
--select @Card as 卡号
--8、    开户功能
if exists(select * from sysobjects where name = 'P_AccountOpening')
    drop proc P_AccountOpening
go
create proc P_AccountOpening
    ),
    @Sex bit,
    ),
    ),
    ),
    ) output
as
    if exists (select * from Xxl_UserInfo where Xxl_User_IDcard =@IdCard)
    begin
        set @errMeg =  '存在此账户!'

    end
    begin tran
    declare @UserID int

    insert Xxl_UserInfo values(@Name,@Sex,@IdCard,@Moblie,@Address)
    select @err =  @@ERROR + @err
    )
    exec P_GenerateBankcard @Card output
    select @UserID = Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = @IdCard
    ,,'使用')
    select @err =  @@ERROR + @err
    )
    begin
        set @errMeg =  '开户成功!'
        commit tran

    end
    else
    begin
        set @errMeg =  '未知错误!'
        rollback tran

    end
go
--9、    解冻功能
if exists(select * from sysobjects where name = 'P_ThawAccount')
    drop proc P_ThawAccount
go
create proc P_ThawAccount
    )
as
     where Xxl_Card_No = @CardNo

go
--10、    根据用户身份证,查询该用户下所有的银行卡信息
if exists(select * from sysobjects where name = 'P_SelectCard')
    drop proc P_SelectCard
go
create proc P_SelectCard
    )
as
    )
    select @ID = Xxl_User_Id from Xxl_UserInfo where Xxl_User_IDcard = @IdCard
    select * from Xxl_CardInfo where From_Xxl_User_Id = @ID
go
--------------------------------------------结束--------------------------------------------

ATM-简单SQL查询的更多相关文章

  1. kotlin 写的一个简单 sql 查询解析器

    package com.dx.efuwu.core import org.apache.commons.lang.StringUtils import java.sql.PreparedStateme ...

  2. 记一个简单的sql查询

    在我们做各类统计和各类报表的时候,会有各种各样的查询要求.条件 这篇主要记录一个常见的统计查询 要求如下: 统计一段时间内,每天注册人数,如果某天没有人注册则显示为0 现在建个简单的表来试试 建表语句 ...

  3. 发送json-简单的传参查询和简单的sql查询

    简单的传参查询并转化为json using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  4. 一条简单的 SQL 查询语句到底经历了什么?

    一.MySQL 基础架构   整体来说 MySQL 主要分为两个部分,一个部分是:Server 层,另一部分是:存储引擎层. 其中 Server 层包括有连接器.查询缓存.分析器.优化器.执行器等,存 ...

  5. sql最简单的查询语句

    -- 2 **************************************************** -- 最简单的查询语句 -- 2.1 ----------------------- ...

  6. 简单记录几个有用的sql查询

    转载自:http://blog.itpub.net/16436858/viewspace-676265/ 下面示例中,查询的数据表参考这一篇的Person表. 一.限制返回的行数 1.Sql Serv ...

  7. sql查询语句如何解析成分页查询?

    我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法.sql查询 语句如何解析成“分页查询”和“总统计”两条语句.可能, ...

  8. Hibernate SQL查询 addScalar()或addEntity()

    本文完全引用自: http://www.cnblogs.com/chenyixue/p/5601285.html Hibernate除了支持HQL查询外,还支持原生SQL查询.          对原 ...

  9. 提高SQL查询效率(SQL优化)

    要提高SQL查询效率where语句条件的先后次序应如何写 http://blog.csdn.net/sforiz/article/details/5345359   我们要做到不但会写SQL,还要做到 ...

  10. 【T-SQL基础】01.单表查询-几道sql查询题

    概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...

随机推荐

  1. javaScript之变量与数据类型

    http://www.cnblogs.com/yuanchenqi/articles/5980312.html 在了解变量之前,我们首先学习JavaScript的引入方式 JavaScript的引入方 ...

  2. js中对象和对象创建方法

    这一次我们来说一说在JavaScript中经常会用到的一个复杂基本类型,对象,先从对象的属性讲起,再讲对象的创建方法,基本涵盖了创建对象的各种方法,大家一起学习呀~ 一.对象 要掌握对象的使用及继承, ...

  3. SHELL脚本--简介

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 脚本都以#!/bin/bash开头,“#”称为sharp,“! ...

  4. Perl面向对象(3):解构——对象销毁

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. perl中使用引用计数的方式管理内存, ...

  5. RNN入门(一)识别MNIST数据集

    RNN介绍   在读本文之前,读者应该对全连接神经网络(Fully Connected Neural Network, FCNN)和卷积神经网络( Convolutional Neural Netwo ...

  6. TypeScript学习之WebStorm(1)

    修改快捷键 本没有想要修改快捷键,调试的时候下意识的 按F10,发现还是习惯使用visual studio的快捷键.WebStorm支持修改快捷键,File=>Settings=>Keym ...

  7. sql server 查询时会锁表吗?

    sql server在执行查询语句时会锁表.在锁表期间禁止增删改操作. 如果不想锁表,那就再表名或别名后面加上WITH(NOLOCK) 如下所示:

  8. vue+elementUI项目,父组件向子组件传值,子组件向父组件传值,父子组件互相传值。

    vue+elementUI项目,父组件向子组件传值,子组件向父组件传值,父子组件互相传值. vue 父组件与子组件相互通信 一.父组件给子组件传值 props 实现父组件向子组件传值. 1父组件里: ...

  9. 在JS方法中返回多个值的三种方法(转载)

    来源:https://www.cnblogs.com/gxsyj/p/6004574.html 在使用JS编程中,有时需要在一个方法返回两个个或两个以上的数据,用下面的几种方法都可以实现: 1 使用数 ...

  10. C#判断输入的是否为数字(int.TryParse)

    了解 TryParse: TryParse静态方法用来将字符串转换成对应类型的数值. 所以int.TryParse()是将字符串转换为int类型的,如果成功返回true,失败返回false. priv ...