建库建表建约束
插入数据

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

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,'',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 '王老五','','2222-63598978',1000,'活期','河南新乡'
exec usp_openAccount '赵小二','','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())+'.'+'')
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())+'.'+'')
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())+'.'+'')
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())+'.'+''+'.'+'')
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())+'.'+''+'.'+'')
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())+'.'+''+'.'+'')
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())+'.'+''+'.'+'')
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','','1010 3576 1212 1130',500
select * from cardInfo

SQL Server 中存储过程的练习的更多相关文章

  1. SQL Server中存储过程比直接运行SQL语句慢的原因

    原文:SQL Server中存储过程比直接运行SQL语句慢的原因 在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1.       存储过程只在创造时进行编译即可,以 ...

  2. SQL Server中存储过程 比 直接运行SQL语句慢的原因

    问题是存储过程的Parameter sniffing     在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1. 存储过程只在创造时进行编译即可,以后每次执行存储过 ...

  3. SQL Server中存储过程与函数的区别

    本质上没区别.只是函数有如:只能返回一个变量的限制.而存储过程可以返回多个.而函数是可以嵌入在sql中使用的,可以在select中调用,而存储过程不行.执行的本质都一样. 函数限制比较多,比如不能用临 ...

  4. SQL Server中存储过程的创建命令

    Create Proc 存储过程名称 ( @参数1 参数类型, @参数2 参数类型, ... ... --最后一行参数,别加逗号了,加逗号的意思是表示后面还有参数 ) AS 需要执行的SQL命令 GO ...

  5. 关于SQL Server中存储过程在C#中调用的简单示例

    目录 0. 简介 1. 语法细节 2. 示例1:模拟转账 3. 示例2:测试返回DataTable 4. 源代码下载 shanzm-2020年5月3日 23:23:44 0. 简介 [定义]:存储过程 ...

  6. SQL SERVER中存储过程IN 参数条件的使用!!!

    正常的传递  @SendStationID='''1'',''2''' 是无效,改用 @SendStationID='1,2,3,003,002' 调用以下的存储过程可以实现in 查询效果 USE [ ...

  7. 【SQL】SQL Server中存储过程的调试方法

    1.以管理员用户登录DB服务器,把域用户追加到「Administrators」组. 2.在本机上以域用户登录,启动VS. 3.追加DB连接 4.右击要debug的存储过程,选择「ストアドプロシージャに ...

  8. SQL Server中对存储过程的理解

    数据库的存储过程理解为,处理数据的子程序,写起来像函数,用起来像函数,在SQL Server中存储过程分为两大类,系统的和自定义的,系统的都放在master系统数据库中,自定义就是自己去写的,用DDL ...

  9. MS SQL Server中数据表、视图、函数/方法、存储过程是否存在判断及创建

    前言 在操作数据库的时候经常会用到判断数据表.视图.函数/方法.存储过程是否存在,若存在,则需要删除后再重新创建.以下是MS SQL Server中的示例代码. 数据表(Table) 创建数据表的时候 ...

随机推荐

  1. leetcode日记 Combination sum IV

    题目: Given an integer array with all positive numbers and no duplicates, find the number of possible ...

  2. cookies的获取,删除,设置

    cookies,sessionStorage 和 localStorage 的区别? 1.cookie在浏览器和服务器间来回传递. sessionStorage和localStorage不会: 2.s ...

  3. Java在JFinal中出现Can not create instance of class: com.keesail.web.config.WebConfig异常处理方式

    编译的时候一直出现如下问题: 后面 查了许多资料 说是build项目的时候web.xml没有输出到class目录.后面试了很多方式不行.后面自己摸索出如下方式解决问题: 改成默认输出目录.

  4. multipath tcp experiment

    git clone https://github.com/Neohapsis/mptcp-abuse.git sudo apt-get install python-pip sudo pip inst ...

  5. HTML5 拖动

    触发的事件有:dragstart事件.drag事件和dragend事件. 按下鼠标键并开始移动鼠标的时候,会在被拖拽的元素上触发dragstart事件.这时候光标变成”不能放”符号(圆环中有一条反斜线 ...

  6. freeCAD定制界面

    由于freecad接口是基于现代Qt工具包,它非常先进.窗口,菜单,工具栏和其他工具都可以修改,移动工作台,共享,键盘快捷键都可以设置.修改,以及宏,它可以录制和播放.定制界面是访问Tools -&g ...

  7. 程序测试用的IE浏览器第二次无法加载入口程序的问题及其解决方法

    注:针对的是C#程序(Silverlight) 第一步.找到入口程序所在的路径,以记事本形式打开<入口程序.csproj>,由于之前配置入口程序时,设置了“Use Local IIS We ...

  8. leetcode216-Combination Sum III

    https://leetcode.com/problems/combination-sum-iii/ 用dfs枚举. class Solution { public: int kk, nn; vect ...

  9. Appweb写法

    web.xml v2.3 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web- ...

  10. iOS8: Ignore manifest download, already have bundleID

    在企业分发的app下载过程中,iOS8发现挂在官网上的企业版的app点击了提示是否安装应用程序,但始终安装不上程序,的device console发现安装的时候出现 LoadExternalDownl ...