-------------------用例4:模拟常规业务---------------------------
-----1.修改客户密码--------------------
--描述:修改张三(卡号为1010357612345678)银行卡密码为123456,修改李四(卡号为1010357612121134)银行卡密码为123123--
update cardInfo set pass='123456 '
where cardID='1010 3576 1234 5678'
update cardInfo set pass='123123 '
where cardID='1010 3576 1212 1130'
----2.办理银行卡挂失-----------------
--描述:李四(卡号为1010357612121134)因银行卡丢失,申请挂失
update cardInfo set IsReportLoss=1
where cardID='1010 3576 1212 1130'
---3.统计银行资金流通金额和盈利结算
--描述: *:存入代表资金流入,支取代表资金流出 计算公式:资金流通金额=总存入金额-总支取金额
-- *:假定存款利率为3%,贷款利率为8% 计算公式:盈利结算=总支取金额*0.008-总存入金额*0.003
declare @inMoney money ,@onMoney money,@M money
select @inMoney=SUM(tradeMoney) from tradeInfo
where (tradeType='存入')
select @onMoney=SUM(tradeMoney) from tradeInfo
where (tradeType='支取')
set @M=@onMoney*0.008-@inMoney*0.003
print '银行流通金额总计为:'+convert(varchar(20),@inMoney-@onMoney)+'RMB'
print '盈利结算为:'+convert(varchar(20),@M)+'RMB'
----4.查询本周开户信息
--描述:查询本周开户卡号,显示该卡的相关信息
select * from cardInfo
where DATEDIFF(DAY,GETDATE(),openDate)<DATEPART(WEEKDAY,openDate)
----5.查询本周交易接最高的卡号
--描述:查询本月存款,取款交易金额最高的卡号信息
--select * from tradeInfo where tradeMoney =(select cardID from cardInfo )
select top 1 cardID 卡号 ,max(tradeMoney) 最高交易金额
from tradeInfo
where datepart(mm,getDate())=datepart(mm,tradeDate)
group by cardID order by max(tradeMoney)desc
----6.查询挂失客户
--查询挂失账号的客户信息
select customerName as 客户信息,PID as 客户身份证号,telephone as 客户电话,address as 客户地址
from userInfo where customerID in
(select userInfo.customerID
from userInfo,cardInfo
where userInfo.customerID=cardInfo.customerID and IsReportLoss=1)
----7.催款提醒业务
--描述:根据某种业务(如代缴电话费,代缴手机费等)的需要,每个月末,若查询出客户账上余额少于200元,则有银行统一致电催款
select customerName as 客户姓名 ,telephone as 客户电话,balance as 存款余额 from userInfo , cardInfo
where cardInfo.customerID=userInfo.customerID and userInfo.customerID in
(select customerID from cardInfo where balance<=200)
--------------------------------------------------------------------------------
-----------------------用例5.创建,使用视图-----------------------------
----
--userInfo
GO
create view vw_userInfo
as
select 客户编号=customerID,开户名=customerName,身份证号=PID,电话号码=telephone,居住地址=address
from userInfo
--cardInfo
GO
create view vw_cardInfo
as
select 卡号=cardInfo.cardID,客户=customerName,货币种类=curID,存款类型=savingName,
开户日期=openDate,余额=balance,密码=pass,是否挂失=IsReportLoss
from cardInfo
inner join tradeInfo on cardInfo.cardID=tradeInfo.cardID
inner join Deposit on cardInfo.savingID=Deposit.savingID
inner join userInfo on userInfo.customerID=cardInfo.customerID
--tradeInfo
go create view vw_tradeInfo
as
select 交易日期=tradeDate,交易类型=tradeType,卡号=tradeInfo.cardID,交易金额=tradeMoney,备注=remark
from tradeInfo
inner join cardInfo on cardInfo.cardID=tradeInfo.cardID
--------------------
/*--1.取钱或存钱的存储过程*/
if exists (select * from sysobjects where name = 'usp_takeMoney')
drop proc usp_takeMoney
go
create procedure usp_takeMoney
@card char(19),
@m money,
@type char(4),
@inputPass char(6)=''
AS
print '交易正进行,请稍后......'
if (@type='支取')
if ((SELECT pass FROM cardInfo WHERE cardID=@card)<>@inputPass )
begin
raiserror ('密码错误!',16,1)
return -1
end
DECLARE @mytradeType char(4),@outMoney MONEY,@myCardID char(19)
SELECT @mytradeType=tradeType,@outMoney=tradeMoney ,@myCardID=cardID FROM tradeInfo where cardID=@card
DECLARE @mybalance money
SELECT @mybalance=balance FROM cardInfo WHERE cardID=@card
if (@type='支取')
if (@mybalance>=@m+1)
update cardInfo set balance=balance-@m WHERE cardID=@myCardID
else
begin
raiserror ('交易失败!余额不足!',16,1)
print '卡号'+@card+' 余额:'+convert(varchar(20),@mybalance)
return -2
end
else
update cardInfo set balance=balance+@m WHERE cardID=@card
print '交易成功!交易金额:'+convert(varchar(20),@m)
SELECT @mybalance=balance FROM cardInfo WHERE cardID=@card
print '卡号'+@card+' 余额:'+convert(varchar(20),@mybalance)
INSERT INTO tradeInfo(tradeType,cardID,tradeMoney) VALUES(@type,@card,@m)
RETURN 0
GO
--调用存储过程取钱或存钱 张三取300,李四存500
--现实中的取款机依靠读卡器读出张三的卡号,这里根据张三的名字查出考号来模拟
declare @card char(19)
select @card=cardID from cardInfo Inner Join userInfo ON
cardInfo.customerID=userInfo.customerID where customerName='张三'
EXEC usp_takeMoney @card,10 ,'支取','123456'
GO
select * from cardInfo Inner Join userInfo ON
cardInfo.customerID=userInfo.customerID where customerName='张三'
declare @card char(19)
select @card=cardID from cardInfo Inner Join userInfo ON
cardInfo.customerID=userInfo.customerID where customerName='李四'
EXEC usp_takeMoney @card,500 ,'存入'
select * from vw_cardInfo
select * from vw_tradeInfo
GO
------------------------------------------------------
----------------------2.产生随机卡号------------
/*--2.产生随机卡号的存储过程(一般用当前月份数\当前秒数\当前毫秒数乘以一定的系数作为随机种子) --*/
if exists (select * from sysobjects where name = 'usp_randCardID')
drop proc usp_randCardID
go
create procedure usp_randCardID @randCardID char(19) OUTPUT
AS
DECLARE @r numeric(15,8)
DECLARE @tempStr char(10)
SELECT @r=RAND((DATEPART(mm, GETDATE()) * 100000 )+ (DATEPART(ss, GETDATE()) * 1000 )
+ DATEPART(ms, GETDATE()) )
set @tempStr=convert(char(10),@r) --产生0.xxxxxxxx的数字,我们需要小数点后的八位数字
set @randCardID='1010 3576 '+SUBSTRING(@tempStr,3,4)+' '+SUBSTRING(@tempStr,7,4) --组合为规定格式的卡号
GO
--测试产生随机卡号
DECLARE @mycardID char(19)
EXECUTE usp_randCardID @mycardID OUTPUT
print '产生的随机卡号为:'+@mycardID
GO
------------------------3.完成开户任务----------------
if exists (select * from sysobjects where name = 'usp_openAccount')
drop proc usp_openAccount
GO
create procedure usp_openAccount @customerName char(8),@PID char(18),@telephone char(13)
,@openMoney money,@savingName char(8),@address varchar(50)=''
AS
DECLARE @mycardID char(19),@cur_customerID int, @savingID int
--调用产生随机卡号的存储过程获得随机卡号
EXECUTE usp_randCardID @mycardID OUTPUT
while exists(SELECT * FROM cardInfo WHERE cardID=@mycardID)
EXECUTE usp_randCardID @mycardID OUTPUT
print '尊敬的客户,开户成功!系统为您产生的随机卡号为:'+@mycardID
print '开户日期'+convert(char(10),getdate(),111)+' 开户金额:'+convert(varchar(20),@openMoney)
IF not exists(select * from userInfo where PID=@PID)
INSERT INTO userInfo(customerName,PID,telephone,address )
VALUES(@customerName,@PID,@telephone,@address)
SELECT @savingID = savingID FROM deposit WHERE savingName =@savingName
-- SELECT savingID FROM deposit WHERE savingName ='活期'
if @savingID is NULL
BEGIN
RAISERROR('存款类型不正确,请重新输入!',16,1)
RETURN -1
END
--PRINT CAST(@savingID AS varchar(10))
select @cur_customerID=customerID from userInfo where PID=@PID
--PRINT CAST(@cur_customerID AS varchar(10))
INSERT INTO cardInfo(cardID,savingID,openMoney,balance,customerID)
VALUES(@mycardID,@savingID,@openMoney,@openMoney,@cur_customerID)
GO
--调用存储过程重新开户
EXEC usp_openAccount '王五','334456889012678','2222-63598978',1000,'活期','河南新乡'
EXEC usp_openAccount '赵二','213445678912342222','0760-44446666',1,'定期'
select * from vw_userInfo
select * from vw_cardInfo
GO
---------------------------------------------------------------------------
---------------------4.分页显示查询交易数据-------------------
/*--4.输入页数和每页显示的记录数,实现分页显示*/
if exists (select * from sysobjects where name = 'usp_pagingDisplay')
DROP PROCEDURE usp_pagingDisplay
GO
CREATE PROCEDURE usp_pagingDisplay
@records int = 10,
@page int = 1
AS
SET NOCOUNT ON
DECLARE @rec1 int
SET @rec1 = @records --@page * @records
DECLARE @rec2 int
SET @rec2 = (@page - 1) * @records
DECLARE @statement nvarchar(200)
SET @statement='SELECT TOP ' + CAST(@rec1 AS varchar(10)) + ' tradeDate 交易日期,tradeType 交易类型,cardID 卡号,trademoney 交易金额 FROM tradeInfo WHERE cardID not in (SELECT TOP '+ CAST(@rec2 AS varchar(10)) + ' cardID FROM tradeInfo)'
--print @statement
EXEC SP_EXECUTESQL @statement,N'@rec1 int,@rec2 int',@rec1,@rec2
GO
--
EXEC usp_pagingDisplay 2,2
-----------------------------------------------------------------------------
------------------------5.打印客户对账单
/*---- 5.打印对账单 ----*/
if exists (select * from sysobjects where name = 'usp_CheckSheet')
drop proc usp_CheckSheet
GO
CREATE PROCEDURE usp_CheckSheet
@cardID varchar(19),
@date1 datetime=NULL,
@date2 datetime=NULL
AS
DECLARE @custName varchar(20)
DECLARE @curName varchar(20)
DECLARE @savingName varchar(20)
DECLARE @openDate datetime
SELECT @cardID=c.cardID, @curName=c.curID, @custName=u.customerName,
@savingName=d.savingName , @openDate=c.openDate
FROM cardInfo c, userInfo u, deposit d
WHERE c.customerID=u.customerID and c.savingID = d.savingID and cardID = @cardID --and u.customerName = user_name()
PRINT '卡号:' + @cardID
PRINT '姓名:' + @custName
PRINT '货币:' + @curName
PRINT '存款类型:' + @savingName
PRINT '开户日期:' + CAST(DATEPART(yyyy,@openDate) AS VARCHAR(4))+'年' + CAST(DATEPART(mm,@openDate) AS VARCHAR(2))+'月' + CAST(DATEPART(dd,@openDate) AS VARCHAR(2))+'日'
PRINT ' '
print '--------------------------------------------------------------------'
IF @date1 IS NULL AND @date2 IS NULL
BEGIN
SELECT tradeDate 交易日, tradeType 类型, tradeMoney 交易金额, remark 备注
FROM tradeInfo
WHERE cardID='1010 3576 1212 1134'--@cardID
ORDER BY tradeDate
RETURN
END
ELSE IF @date2 IS NULL
SET @date2 = getdate()
SELECT tradeDate 交易日, tradeType 类型, tradeMoney 交易金额, remark 备注
FROM tradeInfo
WHERE cardID=@cardID AND tradeDate BETWEEN @date1 AND @date2
ORDER BY tradeDate
GO
--测试打印对帐单
EXEC usp_CheckSheet '1010 3576 1212 1134'
EXEC usp_CheckSheet '1010 3576 1212 1134','2009-11-2','2009-11-30'
------------------------------------------------------------------------------
------------------------------------6.统计未发生交易的账户-----------------------------------
/*--6.查询、统计在指定时间段内没有发生交易的账户信息*/
if exists (select * from sysobjects where name = 'usp_getWithoutTrade')
drop proc usp_getWithoutTrade
GO
create procedure usp_getWithoutTrade
@Num int output,
@Amount decimal(18,2) output,
@date1 datetime = NULL,
@date2 datetime = NULL
AS
IF @date1 IS NULL
BEGIN
declare @dateStr varchar(50)
set @dateStr = convert(varchar(4),DATEPART(YY,GETDATE())) + '-'+convert(varchar(2),DATEPART(mm,GETDATE())) + '-1 00:00:00.000'
set @date1 = convert(datetime, @datestr,101)
END
IF @date2 IS NULL
SET @date2 = getdate()
SELECT distinct u.customerID 客户号,u.customerName 客户姓名,u.PID 身份证号,u.telephone 电话,address 地址
FROM userInfo u
JOIN cardInfo c ON u.customerID = c.customerID
WHERE c.cardID NOT IN (SELECT cardID FROM tradeInfo WHERE tradeDate Between @date1 and @date2)
SELECT @Num=COUNT(customerID), @Amount=SUM(balance)
FROM cardInfo
WHERE cardID NOT IN (SELECT cardID FROM tradeInfo WHERE tradeDate Between @date1 and @date2)
GO
DECLARE @NUM int
DECLARE @Amount decimal(18,2)
DECLARE @date1 datetime
DECLARE @date2 datetime
SET @date1 = '2009-1-1'
SET @date2 = getdate()
EXEC usp_getWithoutTrade @NUM OUTPUT, @Amount OUTPUT--, @date1, @date2
PRINT '统计未发生交易的客户'
PRINT '---------------------------------------'
PRINT '客户人数:' + CAST(@NUM AS varchar(10)) + ' 客户总余额:' + CAST(@Amount AS varchar(20))
------------------------------------------------------------------------
-----------------------------------7.统计银行卡交易量的交易额
------------------
/*--7.统计银行卡交易量和交易额*/
if exists (select * from sysobjects where name = 'usp_getTradeInfo')
drop proc usp_getTradeInfo
GO
create procedure usp_getTradeInfo
@Num1 int output,
@Amount1 decimal(18,2) output,
@Num2 int output,
@Amount2 decimal(18,2) output,
@date1 datetime,
@date2 datetime = NULL,
@address varchar(20) = NULL
AS
-- 初始化变量
SET @Num1 = 0
SET @Amount1 = 0
SET @Num2 = 0
SET @Amount2 = 0
IF @date2 IS NULL
SET @date2 = getdate()
IF @address IS NULL
BEGIN
SELECT @Num1=COUNT(tradeMoney), @Amount1=SUM(tradeMoney)
FROM tradeInfo
WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='存入'
SELECT @Num2=COUNT(tradeMoney), @Amount2=SUM(tradeMoney)
FROM tradeInfo
WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='支取'
END
ELSE
BEGIN
SELECT @Num1=COUNT(tradeMoney), @Amount1=SUM(tradeMoney)
FROM tradeInfo JOIN cardInfo ON tradeInfo.cardID = cardInfo.cardID
JOIN userInfo ON cardInfo.customerID = userInfo.customerID
WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='存入'
AND address Like '%'+@address+'%'
SELECT @Num2=COUNT(tradeMoney), @Amount2=SUM(tradeMoney)
FROM tradeInfo JOIN cardInfo ON tradeInfo.cardID = cardInfo.cardID
JOIN userInfo ON cardInfo.customerID = userInfo.customerID
WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='支取'
AND address Like '%'+@address+'%'
END
GO
--测试
DECLARE @CNT1 int
DECLARE @Total1 decimal(18,2)
DECLARE @CNT2 int
DECLARE @Total2 decimal(18,2)
DECLARE @date1 datetime
DECLARE @date2 datetime
SET @date1 = '2009-1-1'
SET @date2 = getdate()
EXEC usp_getTradeInfo @CNT1 OUTPUT, @Total1 OUTPUT, @CNT2 OUTPUT, @Total2 OUTPUT, @date1, @date2--, '北京'
PRINT '统计银行卡交易量和交易额'
PRINT ''
PRINT '起始日期:' + CONVERT(varchar(10),@date1,102) + ' 截止日期:' + CONVERT(varchar(10),@date2,102)
PRINT '-----------------------------------------------------------'
PRINT '存入笔数:' + CAST(@CNT1 AS varchar(20)) + ' 存入金额:' + CAST(@Total1 AS varchar(20))
PRINT '支取笔数:' + CAST(@CNT2 AS varchar(20)) + ' 支取金额:' + CAST(@Total2 AS varchar(20))
PRINT '-----------------------------------------------------------'
PRINT '发生笔数:' + CAST(@CNT1+@CNT2 AS varchar(20)) + ' 结余金额:' + CAST(@Total1-@Total2 AS varchar(20))
GO
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
-----------------------------------7.用例7:利用事务实现较复杂的数据更新-------------------------
--转帐的事务存储过程
if exists (select * from sysobjects where name = 'usp_tradefer')
drop proc usp_tradefer
GO
create procedure usp_tradefer
@card1 char(19),
@pwd char(6),
@card2 char(19),
@outmoney money
AS
DECLARE @date1 datetime
DECLARE @date2 datetime
SET @date1 = getdate()
begin tran
print '开始转帐,请稍后......'
DECLARE @errors int
set @errors=0
DECLARE @result int
EXEC @result=usp_takeMoney @card1,@outmoney ,'支取',@pwd --'123123'
set @errors=@errors+@@error
if (@errors > 0 or @result <> 0)
begin
print '转帐失败!'
rollback tran
RETURN -1
end
EXEC @result=usp_takeMoney @card2,@outmoney ,'存入'
set @errors=@errors+@@error
if (@errors > 0 or @result <> 0)
begin
print '转帐失败!'
rollback tran
RETURN -1
end
else
begin
print '转帐成功!'
commit tran
SET @date2 = getdate()
print '打印转出账户对账单'
PRINT '-------------------'
EXEC usp_CheckSheet @card1,@date1,@date2
print '打印转入账户对账单'
PRINT '-------------------'
EXEC usp_CheckSheet @card2,@date1,@date2
RETURN 0
end
GO
--测试上述事务存储过程
--从李四的帐户转帐2000到张三的帐户
--同上一样,现实中的取款机依靠读卡器读出张三/李四的卡号,这里根据张三/李四的名字查出考号来模拟
declare @card1 char(19),@card2 char(19)
select @card1=cardID from cardInfo Inner Join userInfo ON
cardInfo.customerID=userInfo.customerID where customerName='李四'
select @card2=cardID from cardInfo Inner Join userInfo ON
cardInfo.customerID=userInfo.customerID where customerName='张三'
--调用上述事务过程转帐
EXEC usp_tradefer @card1,'123123',@card2,2000
select * from vw_userInfo
select * from vw_cardInfo
select * from vw_tradeInfo
GO
优化MySchool 第九章 ATM存取款机
最新推荐文章于 2022-06-07 20:38:44 发布