赞
踩
加油加油 |
本文章主要是在学了SQL Server后做的一个课程设计,全文有三万多字,接近一千行,光简简单单写这篇博客就花了不少时间,若有出现错误之处,请指出,定当感激不尽,一起学习,一起进步。
某银行是一家民办的小型银行企业,现有十多万客户,公司将为该银行开发一套 ATM 存取款机系统,对银行日常的存取款业务进行计算机管理,以便保证数据的安 全性,提高工作效率。
要求根据银行存取款业务需求设计出符合第三范式的数据库结构,使用 T-SQL 语言创建数据库和表,并添加表约束,进行数据的增删改查,运用逻辑结构语句、事 务、视图和存储过程,按照银行的业务需求,实现各项银行日常存款、取款和转账业 务。
该项目的 ATM 存取款机业务如下:
银行为客户提供了各种银行存取款业务。详见表 1
每个客户凭个人身份证在银行可以开设多个银行卡账户,开设账户时,客户需要 提供的开户数据如表 2 所示:
银行为每个账户提供一个银行卡,每个银行卡可以存入一种币种的存款,银行保 存账户如表 3 所示:
客户持银行卡在 ATM 机上输入密码,经系统验证身份后办理存款、取款和转账 等银行业务。银行规定,每个账户当前的存款金额不得小于 1 元。
银行在为客户办理业务时,需要记录每一笔账目,账目交易信息如表 4 所示:
该银行要求这套软件实现银行客户的开户、存款、取款、转账和余额查询等业务,使得银行储蓄业务方便、快捷,同事保证银行业务数据的安全性。
为使开发人员尽快了解银行业务,该银行提供了银行卡手工账户和存取款单据的 样本数据,以供项目开发时参考,参加表 5 和表 6 。
使用 Create DataBase 语句创建“ATM 存取款机系统”数据库BankDB,数据文件和日志文件保存在指定目录下文件增长率为 15% 。
--创建BankDB数据库,数据库文件和日志文件均保存在文件夹D:\data下 --文件增长率均为%,数据文件起始大小为MB,日志文件起始大小为MB create database BankDB on primary ( name=N'BankDB', filename=N'C:\data\BankDB.mdf', size=5mb, filegrowth=15% ) log on ( name=N'BankDB_log', filename=N'C:\data\BankDB_log.ldf', size=2mb, filegrowth=15% )
根据银行业务,分析表中每个列相应的约束要求,为每个表添加各种约束。要求创建表时要求检测是否存在表结构,如果存在,则先删除再创建。
use BankDB go --判断银行客户信息表是否存在,若存在则删除 --sysobjects 系统对象表。保存当前数据库的对象。 --OBJECT_ID()根据对象名称返回该对象的id IF EXISTS(SELECT * FROM sysobjects WHERE id=OBJECT_ID(N'BankCustomerInfo')) DROP TABLE BankCustomerInfo --创建BankCustomerInfo表 create table BankCustomerInfo( CustNum int identity(1,1) primary key not null, --客户编号 CustName char(20) not null, --客户姓名 CustID char(18) not null, --身份证号 CustTelephone char(20) not null, --客户电话 CustAddress varchar(100) not null --客户住址 ) --判断银行卡表是否存在,若存在则删除 IF EXISTS(SELECT * FROM sysobjects WHERE id=OBJECT_ID(N'BankCardInfo')) DROP TABLE BankCardInfo --创建BankCardInfo表 create table BankCardInfo( CardID char(19) primary key not null, --银行卡号 Password char(6) not null, --密码 MoneyType char(5) not null, --货币类型 DepositType int not null, --存款类型 OpenDate date not null, --开户日期 OpenMoney money not null, --开户金额 CardLoss char(10) not null, --是否挂失 CustNum int not null, --客户编号 CardMoney numeric(8,2) not null --卡内余额 ) --判断银行交易信息表是否存在,若存在则删除 IF EXISTS(SELECT * FROM sysobjects WHERE id=OBJECT_ID(N'BankDealInfo')) DROP TABLE BankDealInfo --创建BankDealInfo表 create table BankDealInfo( DealNum int identity(1,1) primary key not null, --交易编号 CardID char(19) not null, --银行卡号 DealDate date not null, --交易日期 DealMoney decimal(8,2) not null, --交易金额 DealType char(256) not null, --交易类型 DealNote varchar(1024) null --交易备注 ) --判断业务类型表是否存在,若存在则删除 IF EXISTS(SELECT * FROM sysobjects WHERE id=OBJECT_ID(N'BankBusinessType')) DROP TABLE BankBusinessType --创建BankBusinessType表 create table BankBusinessType( BusNum int identity(1,1) primary key not null, --业务编号 BusName char(20) not null, --业务名称 BusDescribe varchar(100) null --业务描述 ) --为BankCustomerInfo表添加约束 --添加check约束,身份证号前位必须是数字,后位可以是数字或者X alter table BankCustomerInfo add constraint ck_CustID check(left(CustID ,17) like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' and (right(CustID ,1) like'[0-9]' or right(CustID,1) like 'X')) --客户电话必须是固定电话号码或者手机号 alter table BankCustomerInfo add constraint ck_CustTelephone check( CustTelephone like'[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or CustTelephone like '[0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' or CustTelephone like '1[358][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') --银行卡号前位必须为,后位为-9任意数字 --为BankCardInfo表添加约束 alter table BankCardInfo add constraint ck_CardID check(CardID LIKE '1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]') --密码默认为 alter table BankCardInfo add constraint df_Password default('888888') for Password --货币类型默认为RMB alter table BankCardInfo add constraint df_MoneyType default('RMB') for MoneyType --开户日期默认为当前时间 alter table BankCardInfo add constraint df_OpenDate default(getdate()) for OpenDate --开户金额至少为元 alter table BankCardInfo add constraint ck_OpenMoney check(OpenMoney>=1) --是否挂失默认为’否‘ alter table BankCardInfo add constraint df_CardLoss default('否') for CardLoss
添加子表外键约束
--添加外键约束
alter table BankCardInfo add constraint fk_DepositType foreign key(DepositType) references BankBusinessType(BusNum)
alter table BankCardInfo add constraint fk_CustNum foreign key(CustNum) references BankCustomerInfo(CustNum)
alter table BankDealInfo add constraint fk_CardID foreign key(CardID) references BankCardInfo(CardID)
在 SQL SERVER 里自动生成数据库关系图,如下图所示:
创建 Insert 触发器:
在交易信息表创建一个 insert 触发器,当增加一条交易信息时,修改相应银行卡
的存款余额。
--在交易信息表插入触发器.使用游标当新增一个交易信息.修改银行卡的存款余额 --检查触发器是否存在.存在则删除 if(object_id('tr_insertDealInfo','tr') is not null) drop trigger tr_insertDealInfo go --创建insert 触发器 create trigger tr_insertDealInfo on BankDealInfo for insert as declare @type char(10),@sum money,@BDCardID char(19) --定义游标,指向inserted 表 declare cursor_BankDealInfo cursor for select DealType,DealMoney,CardID from inserted --打开游标、 open cursor_BankDealInfo --读取游标.赋值给相应字段 fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID --@@fetch_status=0 fetch 语句成功 while @@fetch_status=0 begin --根据交易类型.更新银行卡余额.rtrim 和lraim 是去除首尾空格 if(rtrim(ltrim(@type))='存入') update BankCardInfo set CardMoney=CardMoney+@sum where CardID=@BDCardID if(rtrim(ltrim(@type))='支取') update BankCardInfo set CardMoney=CardMoney-@sum where CardID=@BDCardID fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID end --关闭游标 close cursor_BankDealInfo --释放游标 deallocate cursor_BankDealInfo go
创建Delete触发器:
在交易信息表创建一个 Delete 触发器,当删除一条交易信息时,修改相应银行卡
的存款余额。
--触发器存在则删除 if(object_id('tr_delDealInfo','tr') is not null) drop trigger tr_delDealInfo go --创建delete 触发器 create trigger tr_delDealInfo on BankDealInfo for delete as declare @type char(10),@sum money,@BDCardID char(19) --定义游标,指向deleted 表 declare cursor_BankDealInfo cursor for select DealType,DealMoney,CardID from deleted --打开游标 open cursor_BankDealInfo --读取游标,赋值 fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID while @@fetch_status=0 begin if(rtrim(ltrim(@type))='存入') update BankCardInfo set CardMoney=CardMoney-@sum where CardID=@BDCardID if(rtrim(ltrim(@type))='支取') update BankCardInfo set CardMoney=CardMoney+@sum where CardID=@BDCardID fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID end close cursor_BankDealInfo deallocate cursor_BankDealInfo go
创建 Update 触发器
在交易信息表创建一个 Update 触发器,当更新一条交易信息时,修改相应银行 卡的存款余额。
--触发器存在则删除 if(object_id('tr_updateDealInfo','tr') is not null) drop trigger tr_updateDealInfo go create trigger tr_updateDealInfo on BankDealInfo for update as declare @type char(10),@sum money,@BDCardID char(19) --定义游标,指向deleted 表 declare cursor_BankDealInfo cursor for select DealType,DealMoney,CardID from deleted --打开游标 open cursor_BankDealInfo --读取游标,赋值 fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID while @@fetch_status=0 begin if(rtrim(ltrim(@type))='存入') update BankCardInfo set CardMoney=CardMoney-@sum where CardID=@BDCardID if(rtrim(ltrim(@type))='支取') update BankCardInfo set CardMoney=CardMoney+@sum where CardID=@BDCardID fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID end close cursor_BankDealInfo deallocate cursor_BankDealInfo --定义游标,指向inserted 表 declare cursor_BankDealInfo cursor for select DealType,DealMoney,CardID from inserted --打开游标 open cursor_BankDealInfo --读取游标.赋值给相应字段 fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID --@@fetch_status=0 fetch 语句成功 while @@fetch_status=0 begin --根据交易类型.更新银行卡余额.rtrim 和lraim 是去除首尾空格 if(rtrim(ltrim(@type))='存入') update BankCardInfo set CardMoney=CardMoney+@sum where CardID=@BDCardID if(rtrim(ltrim(@type))='支取') update BankCardInfo set CardMoney=CardMoney-@sum where CardID=@BDCardID fetch next from cursor_BankDealInfo into @type,@sum,@BDCardID end --关闭游标 close cursor_BankDealInfo --释放游标 deallocate cursor_BankDealInfo
使用 T-SQL 语句向每个表插入如下所示测试数据,要保证业务数据的一致性和 完整性。
BankBusinessType 表的测试数据:
insert into BankBusinessType
(BusName,BusDescribe)
values ('活期','无固定存期,可随时存取,存取金额不限的一种比较灵活的存款'),
('定活两便','事先不约定存定期,一次性存入,一次性支取的存款'),
('通知','不约定存期,支取是需提前通知银行,约定支取日期和金额方能支取的存款'),
('整存整取年','整笔存入,到期提取本息'),
('整存整取年','整笔存入,到期提取本息'),
('整存整取年','整笔存入,到期提取本息'),
('零存整取年','事先原定金额,逐月按约定金额存入,到期支付本息'),
('零存整取年','事先原定金额,逐月按约定金额存入,到期支付本息'),
('零存整取年','事先原定金额,逐月按约定金额存入,到期支付本息'),
('自助转账','银行atm 存取款机上办理银行卡之间互相划转')
go
select * from BankBusinessType
BankCustomerInfo 表的测试数据:
insert into BankCustomerInfo(CustName,CustID,CustTelephone,CustAddress)
values ('叶春萌','152825198512548541','13585941287','丽都新城'),
('周明','152821198401248574','0147-7418527','松石名第')
go
select * from BankCustomerInfo
BankCardInfo表的测试数据:
insert into BankCardInfo
(CardID,Password,MoneyType,DepositType,OpenDate,OpenMoney,CardLoss,CustNum,CardMoney)
values ('1010 3576 1234 5678','197611','RMB',1,cast(dateadd(day,-(rand()*30),getdate())
as date),1000,'否',1,1000),
('1010 3576 1234 5688','197611','RMB',2,cast(dateadd(day,-(rand()*30),getdate())
as date),1000,'否',2,1500)
go
select * from BankCardInfo
BankDealInfo 表的测试数据:
insert into BankDealInfo
(CardID,DealDate,DealMoney,DealType,DealNote)
values ('1010 3576 1234 5678',cast(dateadd(day,-(rand()*15),getdate()) as date),500,'存入','单位月工资'),
('1010 3576 1234 5678',cast(dateadd(day,-(rand()*15),getdate()) as date),1500,'存入','单位月工资'),
('1010 3576 1234 5678',cast(dateadd(day,-(rand()*15),getdate()) as date),300,'存入','支付宝付款'),
('1010 3576 1234 5678',cast(dateadd(day,-(rand()*15),getdate()) as date),400,'支取','刷卡消费'),
('1010 3576 1234 5688',cast(dateadd(day,-(rand()*15),getdate()) as date),3500,'存入','单位月工资'),
('1010 3576 1234 5688',cast(dateadd(day,-(rand()*15),getdate()) as date),4500,'存入','单位月工资'),
('1010 3576 1234 5688',cast(dateadd(day,-(rand()*15),getdate()) as date),800,'存入','支付宝付款'),
('1010 3576 1234 5688',cast(dateadd(day,-(rand()*15),getdate()) as date),900,'支取','刷卡消费')
go
select * from BankDealInfo
根据卡号修改指定 2 个客户的银行密码,其中第一个客户 1010 3576 1234 5678 密码修改为 123456,第二个客户 1010 3576 1234 5688 修改为 123123。
update BankCardInfo set Password='123465' where CardID='1010 3576 1234 5678'
update BankCardInfo set Password='123123' where CardID='1010 3576 1234 5688'
select CardID '银行卡卡号',Password '密码',MoneyType '货币类型',DepositType '储蓄种类',OpenDate '
开户日期',OpenMoney '开户金额', CardLoss '是否挂失',CustNum'客户编号',CardMoney '存款金额' from BankCardInfo
卡号为 1010 3576 1234 5678 的银行卡丢失,申请挂失。
update BankCardInfo set CardLoss='是' where CardID='1010 3576 1234 5678'
select CardID '银行卡卡号',Password'密码',MoneyType '货币类型',BusName '储蓄类型',OpenDate '
开户日期',OpenMoney '开户金额', CardLoss '是否挂失',CustName'客户姓名',CardMoney '存款
金额' from BankCardInfo
inner join BankCustomerInfo on BankCardInfo.CustNum=BankCustomerInfo.CustNum
inner join BankBusinessType on BankCardInfo.DepositType=BankBusinessType.BusNum
存入代表资金流入,支取代表资金流出。
计算公式:资金流通余额=总存入金额-总支取金额
假定存款利率为千分之三,贷款利率为千分之八。
计算公式:盈利结算=总支取金额0.008-总存入金额0.003 。
要求创建一个存储过程 proc_staticsBanlanceAndProfit 。
if exists(select * from sys.sysobjects where name='proc_staticsBanlanceAndProfit')
drop procedure proc_staticsBanlanceAndProfit
go
create procedure proc_staticsBanlanceAndProfit
as
declare @inmoney money
declare @outmoney money
select @inmoney=sum(DealMoney) from BankDealInfo where DealType='存入'
select @outmoney=sum(DealMoney) from BankDealInfo where DealType='支取'
print '存入总金额:'+ltrim(str(@inmoney))+'rmb,支取总金
额:'+ltrim(str(@outmoney))+'rmb,银行流通余额:'+ltrim(str(@inmoney-@outmoney))+'rmb,
盈利余额:'+
ltrim(str(@outmoney*0.008-@inmoney*0.003))+'rmb'
go
exec proc_staticsBanlanceAndProfit
查询本周开户的卡号,显示该卡的相关信息。
--默认星期日作为一周的第一天.修改星期一为第一天
set datefirst 1
select CardID '银行卡卡号',CustName '姓名', MoneyType '货币类型',OpenDate '开户时间
',BusName '储蓄类型',OpenMoney '开户金额',CardMoney '存款金额', case CardLoss
when '是' then '挂失账户'
when '否' then '正常账户'
end '是否挂失' from BankCardInfo
inner join BankBusinessType on BankBusinessType.BusNum = BankCardInfo.DepositType
inner join BankCustomerInfo on BankCustomerInfo.CustNum = BankCardInfo.CustNum
where datediff(day,OpenDate,getdate())<datepart(weekday,getdate())
查询本月存、取款中单次交易金额最高的卡号信息。
select distinct BankDealInfo.CardID '银行卡卡号',OpenDate '开户日期',OpenMoney '开户金额
',DealMoney '单次最高金额' from BankCardInfo
inner join BankDealInfo on BankDealInfo.CardID=BankCardInfo.CardID
where DealMoney =(select max(DealMoney ) from BankDealInfo
where datediff(month,DealDate,getdate())=0)
select top 1 BankCardInfo.CardID '银行卡卡号',OpenDate '开户日期',OpenMoney '开户金额
',sum(DealMoney ) '总交易最高金额' from BankCardInfo
inner join BankDealInfo on BankDealInfo.CardID=BankCardInfo.CardID
where datediff(month,DealDate,getdate())=0
group by BankCardInfo.CardID,OpenDate,OpenMoney
order by sum(DealMoney ) desc
--子查询
select CustName '客户姓名',CustTelephone '联系电话' from BankCustomerInfo
where CustNum in (select CustNum from BankCardInfo where CardLoss='是')
--内连接
select CustName '客户姓名',CustTelephone '联系电话' from BankCustomerInfo
inner join BankCardInfo on BankCardInfo.CustNum = BankCustomerInfo.CustNum
where CardLoss='是'
根据某种业务(如代缴电话费、代缴手机费或房贷等)的需要,每个月末,查询出客户账户上余额少于 10000 元,由银行统一致电催款。
select CustName '客户姓名',CustTelephone '联系电话',CardMoney '存款金额' from BankCardInfo
inner join BankCustomerInfo on BankCustomerInfo.CustNum = BankCardInfo.CustNum
where CardMoney<=10000
显示的列名全为中文,要求先判断该视图是否存在,若存在,则先删除。
if object_id('vw_userinfo','v') is not null
drop view vw_userinfo
go
create view vw_userinfo
as
select CustNum '客户编号',CustName '开户名',CustID '身份证号',CustTelephone '电话号码',CustAddress '居住地址'
from BankCustomerInfo
go
select * from vw_userinfo
if object_id('VW_CardInfo','v') is not null
drop view VW_CardInfo
go
create view VW_CardInfo
as
select CardID '银行卡卡号',CustName '姓名', MoneyType '货币类型',BusName '储蓄类型
',OpenDate '开户日期',CardMoney '存款金额',Password'密码', CardLoss '是否挂失' from
BankCardInfo
inner join BankCustomerInfo on BankCustomerInfo.CustNum = BankCardInfo.CustNum
inner join BankBusinessType on BankBusinessType.BusNum = BankCardInfo.DepositType
go
select * from VW_CardInfo
if object_id('VW_TransInfo','v') is not null
drop view VW_TransInfo
go
create view VW_TransInfo
as
select DealDate '交易日期',DealType '交易类型',CardID '银行卡卡号',DealMoney'交易
金额',DealNote '备注' from BankDealInfo
go
select * from VW_TransInfo order by 交易日期
根据客户登录名(采用实名制访问银行系统)查询该客户账户信息的视图,利用SQL SERVER 系统函数 system_user 获得数据库用户名。
if object_id('VW_OneUserInfo','v') is not null
drop view VW_OneUserInfo
go
create view VW_OneUserInfo
as
select CustNum '客户编号',CustName '开户名',CustID '身份证号',CustTelephone '电话号码',CustAddress '居住地址'
from BankCustomerInfo
where CustName=system_user
go
select * from VW_OneUserInfo
描述:
根据银行卡号和交易金额实现银行卡的存款和取款业务。
每一笔存款,取款业务都要计入银行交易账,并同时更新客户的存款余额。
如果是取款业务,在记账之前,要完成下面两项数据的检查验证工作,如果检查 不合格,那么中断取款业务,给出提示信息后退出。
检查客户输入的密码是否正确。
账户取款金额是否大于当前存款额加 1 。
--判断存储过程是否存在.存在则删除 if object_id('proc_TakeMoney','p') is not null drop procedure proc_TakeMoney go --创建存取款业务的存储过程 create procedure proc_TakeMoney @CardID char(19),@money money,@pwd char(6)=null as --不返回受影响的行数 set nocount on declare @existbanlance money --启动事务 begin transaction select @existbanlance=CardMoney from BankCardInfo where CardID=@CardID print '交易前,卡号'+@CardID print '交易正进行,请稍后...' --如果输入参数@pwd 为空,则为取款业务.否则为存款业务 if (select CardLoss from BankCardInfo where CardID=@CardID)='是' begin print '本卡已挂失,不能交易!' rollback transaction return end if(@pwd is not null) begin --取款 if exists(select * from BankCardInfo where CardID=@CardID and Password=@pwd) begin if(@money<=@existbanlance-1) insert into BankDealInfo (CardID,DealDate,DealMoney,DealType,DealNote) values (@CardID,getdate(),@money,'支取','通过存储过程操作') else print '取款交易失败.余额不足,请减少取款' end else begin print '取款交易失败,卡号或密码有错误' rollback transaction return end end else begin --办理存款业务 if exists(select * from BankCardInfo where CardID=@CardID) insert into BankDealInfo (CardID,DealDate,DealMoney,DealType,DealNote) values (@CardID,getdate(),@money,'存入','通过存储过程操作') else begin print '存款失败,卡号不存在' rollback transaction return end end --判断事务处理是否有异常.没有提交.有则回滚 if(@@error<>0) begin print '交易失败,发生未知错误' rollback transaction end else begin commit transaction print '交易成功,交易金额为:'+ltrim(str(@money)) select @existbanlance=CardMoney from BankCardInfo where CardID=@CardID print '卡号:'+@CardID+',余额为:'+ltrim(str(@existbanlance)) end --显示银行卡用户详情和交易详情 select * from VW_CardInfo select * from VW_TransInfo order by 交易日期 go --执行存款存储过程 exec proc_TakeMoney @CardID='1010 3576 1234 5688',@money=2000 --执行取款存储过程 exec proc_TakeMoney @CardID='1010 3576 1234 5688',@money=1200,@pwd='123465'
创建存储过程产生 8 位随机数字,与前 8 位固定数字“1010 3576”连接,生成一个由 16 位数字组成的银行卡号,并输出。
if object_id('proc_RandCardID','p') is not null drop procedure proc_RandCardID go --创建随机卡号的存储过程 create procedure proc_RandCardID @randcardid char(19) output as declare @r numeric(8,8),@tmpstr char(10) --产生随机种子=当前的月份数*100000+当前的秒数*1000+当前的毫秒数 set @r=rand(datepart(month,getdate())*100000+datepart(second,getdate())*1000+datepart(millisecond,getdate())) set @tmpstr=convert(char(10),@r) set @randcardid='1010 3576 '+substring(@tmpstr,3,4)+' '+substring(@tmpstr,7,4) go declare @mycardid1 char(19) exec proc_RandCardID @mycardid1 output print '产生随机卡号为'+@mycardid1
描述:
利用存储过程为客户开设 2 个银行卡账户,开户时需要提供客户的信息有:开户 名、身份证号、电话号码、开户金额、存款类型和地址。客户的信息见表所示:
为成功开户的客户提供银行卡,且银行卡号唯一。
if object_id('proc_OpenAccount','p') is not null drop procedure proc_OpenAccount go --创建存取款业务的存储过程 create procedure proc_OpenAccount @CustName char(20),@CustID char(18),@CustTelephone varchar(20),@OpenMoney money,@BusName varchar(20),@CustAddress varchar(100) as --不返回受影响的行数 set nocount on declare @DepositType int,@CardID char(19),@CustNum int --判断存款类型是否正确 if exists(select * from BankBusinessType where BusName=@BusName) begin begin tran select @DepositType=BusNum from BankBusinessType where BusName=@BusName exec proc_randcardid @CardID output --选出不重复的卡号 while(exists(select * from BankCardInfo where CardID=@CardID)) exec proc_RandCardID @CardID output --插入一条客户信息记录,身份证一样不重复插入 if not exists(select * from BankCustomerInfo where CustID=@CustID ) begin insert BankCustomerInfo (CustName,CustID,CustTelephone,CustAddress) values (@CustName,@CustID,@CustTelephone,@CustAddress) set @CustNum=@@identity end else select @CustNum=CustNum from BankCustomerInfo where CustID=@CustID insert BankCardInfo (CardID,Password,MoneyType,DepositType,OpenDate,OpenMoney,CardLoss,CustNum,CardMoney) values (@CardID,'888888','rmb',@DepositType,getdate(),@OpenMoney,'否 ',@CustNum,@OpenMoney) if(@@error<>0) begin print '尊敬的客户,开户不成功,所有操作均撤销' rollback tran end else begin commit tran print '尊敬的客户,开户成功,系统为你产生的随机卡号是:'+@CardID+',开户日 期:'+convert(char(10),getdate(),111)+',开户金额:'+ltrim(str(@OpenMoney)) end --显示开户的客户表信息和银行卡信息 select * from vw_userinfo select * from VW_CardInfo end else print '尊敬的客户,未能成功开户,存款类型不正确,请重新输入!' go exec proc_OpenAccount @CustName = '陈曦',@CustID = '152825198909120124',@CustTelephone = '13574129856',@OpenMoney = 1200,@BusName = '定活两便',@CustAddress = '海天家园' exec proc_OpenAccount @CustName = '林念初',@CustID = '152825197905149652',@CustTelephone = '15852147869',@OpenMoney = 1000,@BusName = '活期',@CustAddress = '峻峰华庭'
根据指定的页数和每页的记录数分页显示交易数据。
if object_id('proc_PagingDisplay','p') is not null
drop procedure proc_PagingDisplay
go
create procedure proc_PagingDisplay
@page int,@count int
as
select rownumber '交易编号',DealDate '交易日期',DealType '交易类型',CardID '银行卡卡号
',DealMoney'交易金额' from (select row_number() over(order by DealNum)
rownumber,DealDate,DealType,CardID,DealMoney from BankDealInfo) c
where c.rownumber between (@page-1)*@count+1 and @page*@count
go
exec proc_PagingDisplay @page = 2,@count = 5
查询统计指定时间段内没有发生交易的账户信息
if object_id('proc_getwithouttrade','p') is not null drop procedure proc_getwithouttrade go create procedure proc_getwithouttrade @startdate datetime=null,@enddate datetime=null as declare @name char(16),@icno char(18),@tel char(15),@addr char(50),@moneysum money=0,@customersum int=0,@money int if (@startdate is null) set @startdate=convert(datetime,convert(char(8),getdate(),120)+'1') if (@enddate is null) set @enddate=getdate() --有的客户未交易.所以用右连接或者全连接 declare cur_outtrade cursor for select distinct CustName,CustID,CustTelephone,CustAddress,CardMoney from BankDealInfo right join BankCardInfo on BankCardInfo.CardID=BankDealInfo.CardID right join BankCustomerInfo on BankCustomerInfo.CustNum = BankCardInfo.CustNum where BankDealInfo.CardID not in(select BankDealInfo.CardID from BankDealInfo where DealDate>=@startdate and DealDate<=@enddate) print convert(char(16),'客户姓名')+convert(char(20),'身份证号')+convert(char(16),'电 话')+convert(char(20),'地址') open cur_outtrade fetch next from cur_outtrade into @name,@icno,@tel,@addr,@money while @@fetch_status=0 begin print convert(char(16),@name)+convert(char(20),@icno)+convert(char(16),@tel)+convert(char(20),@addr) set @moneysum=@moneysum+@money set @customersum=@customersum+1 fetch next from cur_outtrade into @name,@icno,@tel,@addr,@money end print '统计未发生交易的客户' print '客户人数:'+ltrim(str(@customersum))+',客户总余额:'+ltrim(str(@moneysum)) close cur_outtrade deallocate cur_outtrade go exec proc_getwithouttrade @startdate='2015-8-19',@enddate='2015-9-19' exec proc_getwithouttrade
使用存储过程和事务实现转账业务,操作步骤如下所示:
(1)从某一个账户支取一定金额的存款。
(2)将支取金额存入到另一个指定的账户中。
(3)分别打印此笔业务的转出账单和转入账单
--判断该存储过程是否存在,若存在,则删除 if object_id('usp_transfer','p') is not null drop procedure usp_transfer go --创建转账存储过程,需要传递两个账户号码及转账金额 create procedure usp_transfer @outCardID char(19),@inCardID char(19),@dealacount money as --不返回受影响的行数 set nocount on --声明个变量分别存放转出账户和转入账户的转账之后的余额 declare @outexistbalance money,@inexistbalance money --声明变量存放转出账户的姓名、货币类型、存款类型和开户日期 declare @outCustName char(20),@outMoneyType char(5),@outBusName char(20),@outOpenDate date --声明变量存放转入账户的姓名、货币类型、存款类型和开户日期 declare @inCustName char(20),@inMoneyType char(5),@inBusName char(20),@inOpenDate date --声明交易日.类型.交易金额.备注 declare @DealDate date,@DealType char(20),@DealMoney money,@DealNote char(100) print '开始转账,请稍后...' --判断转出账户及余额是否大于转出金额+1 if exists(select * from BankCardInfo where CardID=@outCardID and CardMoney>=@dealacount+1) begin --判断转入账户是否存在 if not exists(select * from BankCardInfo where CardID=@inCardID) begin print '转入账户不存在,转账交易失败' return end --判断转出账户是否存在 if not exists(select * from BankCardInfo where CardID=@outCardID) begin print '转出账户不存在,转账交易失败' return end --启动事务机制 begin tran print '交易正进行,请稍后...' --首先增加一条转出账户的支出交易记录 insert into BankDealInfo (CardID,DealDate,DealMoney,DealType,DealNote) values (@outCardID,getdate(),@dealacount,'支取','转出') -- 给交易日.类型.交易金额.备注赋值 select @DealDate=DealDate,@DealType=DealType,@DealMoney=DealMoney,@DealNote=DealNote from BankDealInfo where DealNum=@@identity --增加一条转入账户的存取交易记录 insert into BankDealInfo (CardID,DealDate,DealMoney,DealType,DealNote) values (@inCardID,getdate(),@dealacount,'存入','转入') --取得转账后两个账户的余额 select @outexistbalance=(select CardMoney from BankCardInfo where CardID=@outCardID) select @inexistbalance=(select CardMoney from BankCardInfo where CardID=@inCardID) --判断事务处理里是否有异常,若没有异常,则提交,若有异常,则回滚 if @@error<>0 begin print '转账交易失败' rollback tran end else begin commit tran print '交易成功,交易金额:'+convert(varchar(10),@dealacount) print '卡号'+@outCardID+' 余额:'+ltrim(str(@outexistbalance)) print '卡号'+@inCardID+' 余额:'+ltrim(str(@inexistbalance)) print '转账成功!' end end --分别打印转出账户对账单和转入账户对账单 else begin print '转出账户余额不足,此次转账交易失败' return end print '打印转出账户对账单' --获取转出账户的相关信息 select @outCustName=CustName,@outMoneyType=MoneyType,@outBusName=BusName,@outOpenDate=OpenDate from BankCardInfo inner join BankCustomerInfo on BankCustomerInfo.CustNum = BankCardInfo.CustNum inner join BankBusinessType on BankBusinessType.BusNum = BankCardInfo.DepositType where CardID=@outCardID print '---------------------------------------------------' print '卡号:'+@outCardID print '姓名:'+@outCustName print '存款类型:'+@outMoneyType print '开户日期:'+convert(char(14),@outOpenDate) print convert(char(15),'交易日')+convert(char(15),'类型')+convert(char(15),'交易金额 ')+convert(char(15),'备注') print '---------------------------------------------------' print convert(char(15),@DealDate)+convert(char(15),@DealType)+convert(char(15),ltrim(@DealMoney))+convert(char(15),@DealNote) print '打印转入账户对账单' --获取转入账户的相关信息 select @inCustName=CustName,@inMoneyType=MoneyType,@inBusName=BusName,@inOpenDate=OpenDate from BankCardInfo inner join BankCustomerInfo on BankCustomerInfo.CustNum = BankCardInfo.CustNum inner join BankBusinessType on BankBusinessType.BusNum = BankCardInfo.DepositType where CardID=@inCardID print '---------------------------------------------------' print '卡号:'+@inCardID print '姓名:'+@inCustName print '存款类型:'+@inMoneyType print '开户日期:'+convert(char(14),@inOpenDate) -- 给交易日.类型.交易金额.备注赋值 select @DealDate=DealDate,@DealType=DealType,@DealMoney=DealMoney,@DealNote=DealNote from BankDealInfo where DealNum=@@identity print convert(char(15),'交易日')+convert(char(15),'类型')+convert(char(15),'交易金额 ')+convert(char(15),'备注') print '---------------------------------------------------' print convert(char(15),@DealDate)+convert(char(15),@DealType)+convert(char(15),ltrim(@DealMoney))+convert(char(15),@DealNote) Go exec usp_transfer @outCardID='1010 3576 1234 5678',@inCardID='1010 3576 1234 5688',@dealacount=50
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。