SQL Server 中存储过程的练习


建库建表建约束
插入数据

--建库建表建约束和插入测试数据

use bankDB
go

--1.完成存款,取款业务
--存款

create proc usp_takeMoney
@takeType nvarchar(2),@Money money,@cardID char(19),@pass char(6)=null,@remark text =null
as
print '交易正在进行,请稍后......'
if((select COUNT(1) from cardInfo where cardID=@cardID)=0)
begin
    raiserror('卡号不存在!',16,1)
end
else
--开始事务处理
begin
    --存款
    if(@takeType='存入')
    begin
        declare @errorSum int
        set @errorSum=0
        begin tran
        insert into tradeInfo values(GETDATE(),'存入',@cardID,@Money,@remark)
        set @errorSum+=@@ERROR 
        update cardInfo set balance+=@Money where cardID=@cardID
        set @errorSum+=@@ERROR
        if(@errorSum<>0)
        begin
            rollback tran
            raiserror('交易失败,未知错误!',16,1)
        end
        else
        begin
            commit tran
            print '交易成功!交易金额:'+convert(nvarchar,@Money)
        end
    end
    --取款
    else
    begin
        if(@pass=(select pass from cardInfo where cardID=@cardID))
        begin
            if((select balance from cardInfo where cardID=@cardID)>@Money)
            begin
                declare @errorSum2 int
                set @errorSum2=0
                begin tran
                insert into tradeInfo values(GETDATE(),'支取',@cardID,@Money,@remark)
                set @errorSum2+=@@ERROR
                update cardInfo set balance-=@Money where cardID=@cardID
                set @errorSum2+=@@ERROR
                if(@errorSum2<>0)
                begin
                    rollback tran
                    raiserror('交易失败!未知错误!',16,1)
                end
                else
                begin
                    commit tran
                    print '交易成功!交易金额:'+convert(nvarchar,@Money)
                end
            end
            else
            begin
                raiserror('交易失败!余额不足!',16,1)
            end    
        end
        else
        begin
            raiserror('密码不正确!请检查重输',16,1)
        end
    end
end
go

--检查调用存储过程

exec usp_takeMoney '取出',600,'1010 3576 1234 5678',888888
exec usp_takeMoney '存入',500,'1010 3576 1234 5678',default,'定期存入'
go

--****************************************************************************************************************************************************
--2.产生随机卡号

create proc usp_randCardID
@randCardID char(19) output
as
declare @r numeric(15,8),@tempR varchar(10)
select @r=rand(datepart(mm,getdate())*100000+datepart(ss,getdate())*1000+datepart(ms,getdate()))
set @tempR=convert(char(10),@r)
set @randCardID='1010 3576 '+SUBSTRING(@tempR,3,4)+' '+SUBSTRING(@tempR,7,4)
go

-调用随机卡号存储过程

declare @A varchar(19)
exec usp_randCardID @A output
print @A
go

--****************************************************************************************************************************************************
--3.完成开户业务

create proc usp_openAccount
@name char(8),@identityID char(18),@telPhone char(20),@Money money,@savingName varchar(20),@address varchar(50)=null
as
--判定是否存在输入的存款类型
declare @savingID int,@cardID char(19),@openDate datetime,@openMoney money
if exists(select COUNT(1) from Deposit where savingName=@savingName)
begin
    --将存款类型标号赋值给变量
    select @savingID=(select savingID from Deposit where savingName=@savingName)
    --调用随机卡号存储过程,获取随机卡号
    exec usp_randCardID @cardID output
    declare @errorSum int,@identity int
    set @errorSum=0
    --启动事务
    begin tran
    --向客户表添加数据
    insert into userInfo values(@name,@identityID,@telPhone,@address)
    set @identity=@@IDENTITY
    set @errorSum+=@@ERROR
    --并获取当前日期,传出开户金额参数
    set @openDate=GETDATE()
    set @openMoney=@Money
    --向银行卡信息表添加数据
    insert into cardInfo values(@cardID,'RMB',@savingID,@openDate,@openMoney,@Money,'123456',0,@identity)
    set @errorSum+=@@ERROR
    if(@errorSum<>0)
    begin
        rollback tran
        raiserror('添加客户失败!未知错误!',16,1)
    end
    else
    begin
        commit tran
        print '尊敬的客户,开户成功!系统为您产生的随机卡号为:'+@cardID+'卡户日期:'+convert(varchar,@openDate)+'开户金额:'+convert(nvarchar,@openMoney)
    end
end
else
begin
    raiserror('存款类型不存在!',16,1)
end
go

--调用存储过程

exec usp_openAccount '王老五','334456889012678','2222-63598978',1000,'活期','河南新乡'
exec usp_openAccount '赵小二','213445678912342222','0760-44446666',1,'定期一年'
go

--****************************************************************************************************************************************************
--4.分页显示查询交易数据

create proc usp_pagingDisplay
@page int,@count int
as
select tradeDate, tradeType, cardID, tradeMoney, remark from (
    select *,ROW_NUMBER() over(order by tradeDate) as myid
    from tradeInfo
)as new
where myid between (@page-1)*@count+1 and @page*@count
go

--调用存储过程

exec usp_pagingDisplay 4,2
go

--****************************************************************************************************************************************************
--5.打印客户对账单

create proc usp_CheckSheet
@cardID char(19),@beginTime datetime=null,@endTime datetime=null
as
declare @minDate datetime,@maxDate datetime
select @minDate=MIN(tradedate) from tradeInfo where cardID=@cardID
select @maxDate=MAX(tradedate) from tradeInfo where cardID=@cardID
select * from tradeInfo where cardID=@cardID
and tradeDate between(
    case
        when @beginTime IS NULL then @minDate
        when @beginTime is not null then @beginTime
    end
)and(
    case
        when @endTime IS NULL then @maxDate
        when @endTime is not null then @endTime
    end
)
go

--调用存储过程

exec usp_CheckSheet '1010 3576 1234 5678','2016-08-14 09:59:31.793','2016-08-16 10:00:13.913'
go

--****************************************************************************************************************************************************
--6.统计未发生交易的账户

create proc usp_getWithoutTrade
@beginTime datetime =null,@endTime datetime=null
as
--指定时间没发生交易的客户记录
select * from userInfo 
where customerID in (
    select customerID from cardInfo
    where cardID not in(
        select cardID from tradeInfo
        where tradeDate between(
            case
                when @beginTime IS NULL then convert(datetime,datename(yy,getdate())+'.'+datename(mm,getdate())+'.'+'01')
                when @beginTime is not null then @beginTime
            end
        )and(
            case
                when @endTime IS NULL then GETDATE()
                when @endTime is not null then @endTime
            end
        )
    )
)
declare @peopNum int ,@moneySum money
select @peopNum=COUNT(1) from userInfo 
where customerID in (
    select customerID from cardInfo
    where cardID not in(
        select cardID from tradeInfo
        where tradeDate between(
            case
                when @beginTime IS NULL then convert(datetime,datename(yy,getdate())+'.'+datename(mm,getdate())+'.'+'01')
                when @beginTime is not null then @beginTime
            end
        )and(
            case
                when @endTime IS NULL then GETDATE()
                when @endTime is not null then @endTime
            end
        )
    )
)
select @moneySum=SUM(balance) from cardInfo
where cardID not in(
    select cardID from tradeInfo
    where tradeDate between(
        case
            when @beginTime IS NULL then convert(datetime,datename(yy,getdate())+'.'+datename(mm,getdate())+'.'+'01')
            when @beginTime is not null then @beginTime
        end
    )and(
        case
            when @endTime IS NULL then GETDATE()
            when @endTime is not null then @endTime
        end
    )
)
print '统计未发生交易的客户'
print '--------------------------------------------'
print '客户人数:'+convert(char,@peopNum)+'    客户总余额:'+convert(varchar,@moneySum)
go

--调用存储记录

exec usp_getWithoutTrade '2016-08-16 09:58:36.720','2016-08-16 09:59:40.940'
go

--****************************************************************************************************************************************************
--统计银行卡交易量和交易额

create proc usp_getTradeInfo
@address nvarchar(50),@beginTime datetime=null,@endTime datetime=null
as
declare @insertCount int,@insertSum money,@outCount int,@outSum money
--存入客户人数
select @insertCount=COUNT(1) from tradeInfo
where tradeType='存入'
and cardID in(
    select cardID from cardInfo
    where customerID in(
        select customerID from userInfo
        where address like '%'+@address+'%'
    )
)and tradeDate between(
    case
        when @beginTime IS NULL then convert(datetime,datename(yy,getdate())+'.'+'01'+'.'+'01')
        when @beginTime is not null then @beginTime
    end
)and(
    case
        when @endTime IS NULL then GETDATE()
        when @endTime is not null then @endTime
    end
)
--存入金额总量
select @insertSum=SUM(tradeMoney) from tradeInfo
where tradeType='存入'
and cardID in(
    select cardID from cardInfo
    where customerID in(
        select customerID from userInfo
        where address like '%'+@address+'%'
    )
)and tradeDate between(
    case
        when @beginTime IS NULL then convert(datetime,datename(yy,getdate())+'.'+'01'+'.'+'01')
        when @beginTime is not null then @beginTime
    end
)and(
    case
        when @endTime IS NULL then GETDATE()
        when @endTime is not null then @endTime
    end
)
--存入客户人数
select @outCount=count(1) from tradeInfo
where tradeType='支取'
and cardID in(
    select cardID from cardInfo
    where customerID in(
        select customerID from userInfo
        where address like '%'+@address+'%'
    )
)and tradeDate between(
    case
        when @beginTime IS NULL then convert(datetime,datename(yy,getdate())+'.'+'01'+'.'+'01')
        when @beginTime is not null then @beginTime
    end
)and(
    case
        when @endTime IS NULL then GETDATE()
        when @endTime is not null then @endTime
    end
)
--存入金额总量
select @outSum=SUM(tradeMoney) from tradeInfo
where tradeType='支取'
and cardID in(
    select cardID from cardInfo
    where customerID in(
        select customerID from userInfo
        where address like '%'+@address+'%'
    )
)and tradeDate between(
    case
        when @beginTime IS NULL then convert(datetime,datename(yy,getdate())+'.'+'01'+'.'+'01')
        when @beginTime is not null then @beginTime
    end
)and(
    case
        when @endTime IS NULL then GETDATE()
        when @endTime is not null then @endTime
    end
)
print '存入笔数:'+convert(varchar,@insertCount)+'   '+'存入金额:'+convert(varchar,@insertSum)
print '支取笔数:'+convert(varchar,@outCount)+'   '+'支取金额:'+convert(varchar,@outSum)
go

--调用存储过程

exec usp_getTradeInfo '北京'
go

--****************************************************************************************************************************************************
--利用事务实现较复杂的数据更新

create proc usp_tradefer
@outCardID char(19),@pass char(6),@insertCardId char(19),@tradeMoney money,@remark text=null
as
if(@pass<>(select pass from cardInfo where cardID=@outCardID))
begin
    raiserror('密码错误!',16,1)
    return
end
print '开始转账,请稍候......'
print '交易正在进行,请稍候......'
declare @errorSum int,@dateTime datetime
set @errorSum=0
set @dateTime=GETDATE()
begin tran
update cardInfo set balance-=@tradeMoney where cardID=@outCardID
set @errorSum+=@@ERROR
insert into tradeInfo values (@dateTime,'支取',@outCardID,@tradeMoney,@remark)
set @errorSum+=@@ERROR
update cardInfo set balance+=@tradeMoney where cardID=@insertCardId
set @errorSum+=@@ERROR
insert into tradeInfo values (@dateTime,'存入',@insertCardId,@tradeMoney,@remark)
set @errorSum+=@@ERROR
if(@errorSum<>0)
begin 
    rollback tran
    raiserror('转账失败!未知错误',16,1)
end
else
begin
    commit tran
    declare @balance money
    select @balance=balance from cardInfo where cardId=@outCardId
    print '交易成功!交易金额:'+convert(varchar,@tradeMoney)
    print '转出卡号:'+@outCardId+'   '+'余额:'+convert(varchar,@balance)
    print '转入卡号:'+@insertCardId
    select * from tradeInfo where cardID=@outCardID and tradeDate=@dateTime
    select * from tradeInfo where cardID=@insertCardId and tradeDate=@dateTime
end

--调用存储方法

exec usp_tradefer '1010 3576 1212 1004','888888','1010 3576 1212 1130',500
select * from cardInfo

 

posted @ 2016-08-17 12:19  积点成线  阅读(3287)  评论(0编辑  收藏  举报