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,'',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 中存储过程的练习的更多相关文章
- SQL Server中存储过程比直接运行SQL语句慢的原因
原文:SQL Server中存储过程比直接运行SQL语句慢的原因 在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1. 存储过程只在创造时进行编译即可,以 ...
- SQL Server中存储过程 比 直接运行SQL语句慢的原因
问题是存储过程的Parameter sniffing 在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1. 存储过程只在创造时进行编译即可,以后每次执行存储过 ...
- SQL Server中存储过程与函数的区别
本质上没区别.只是函数有如:只能返回一个变量的限制.而存储过程可以返回多个.而函数是可以嵌入在sql中使用的,可以在select中调用,而存储过程不行.执行的本质都一样. 函数限制比较多,比如不能用临 ...
- SQL Server中存储过程的创建命令
Create Proc 存储过程名称 ( @参数1 参数类型, @参数2 参数类型, ... ... --最后一行参数,别加逗号了,加逗号的意思是表示后面还有参数 ) AS 需要执行的SQL命令 GO ...
- 关于SQL Server中存储过程在C#中调用的简单示例
目录 0. 简介 1. 语法细节 2. 示例1:模拟转账 3. 示例2:测试返回DataTable 4. 源代码下载 shanzm-2020年5月3日 23:23:44 0. 简介 [定义]:存储过程 ...
- SQL SERVER中存储过程IN 参数条件的使用!!!
正常的传递 @SendStationID='''1'',''2''' 是无效,改用 @SendStationID='1,2,3,003,002' 调用以下的存储过程可以实现in 查询效果 USE [ ...
- 【SQL】SQL Server中存储过程的调试方法
1.以管理员用户登录DB服务器,把域用户追加到「Administrators」组. 2.在本机上以域用户登录,启动VS. 3.追加DB连接 4.右击要debug的存储过程,选择「ストアドプロシージャに ...
- SQL Server中对存储过程的理解
数据库的存储过程理解为,处理数据的子程序,写起来像函数,用起来像函数,在SQL Server中存储过程分为两大类,系统的和自定义的,系统的都放在master系统数据库中,自定义就是自己去写的,用DDL ...
- MS SQL Server中数据表、视图、函数/方法、存储过程是否存在判断及创建
前言 在操作数据库的时候经常会用到判断数据表.视图.函数/方法.存储过程是否存在,若存在,则需要删除后再重新创建.以下是MS SQL Server中的示例代码. 数据表(Table) 创建数据表的时候 ...
随机推荐
- 如何在网页中调用百度地图api
我想在木有提供地图接口的年代,前端工程师门要么只写上企业的具体地址,要么就是用一张标有自己位置的地图图片.但是现在不一样啦!为了增强用户体验,谷歌,甚至百度都很开放了,你可以在他们的网站上找到地图接口 ...
- Amoeba for MySQL读写分离配置
一. 安装和运行Amoeba 1. Amoeba for MySQL 架构: 2. 验证Java的安装Amoeba框架是基于Java SE1.5开发的,建议使用Java SE1.5以上的版本 ...
- html canvas 弹球(模仿)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jsp 动态包含和静态包含
jsp其实就是一个servlet或者说一个java文件,做这样三件事:打印html标签到页面,处理逻辑,输出结果 现在有两个jsp文件,包含者1和被包含者2 静态包含,就是把2的java代码塞到1的代 ...
- shopex 小知识
产品链接: http://www.--/product-172.html 中间的数字代表 sdb_goods 表中 的 goods_id ... 表示数据库里的产品 id. 分类链接: http: ...
- css3弹性盒子温习
弹性盒子由弹性容器(Flex container)和弹性子元素(Flex item)组成. 弹性容器通过设置 display 属性的值为 flex 或 inline-flex将其定义为弹性容器. 弹性 ...
- Java web 项目的相对路径的使用
在java Web中有些地方读取文件需要相对路径.在Java Web 中如何使用相对路径呢? Java Web 在发布项目的时候. 发布根路径下WEB-INF/classes 默认使用该方法的路径是: ...
- mysql 新建用户、授权、远程访问
新建用户 insert into mysql.user(Host,User,Password) values("localhost","u",password( ...
- ARMLinux下Alignment trap的一些测试 【转自 李迟的专栏 CSDN http://blog.csdn.net/subfate/article/details/7847356
项目中有时会遇到字节对齐的问题,英文为“Alignment trap”,如果直译,意思为“对齐陷阱”,不过这个说法不太好理解,还是直接用英文来表达. ARM平台下一般是4字节对齐,可以参考文后的给出的 ...
- Sliverlight 样式
UserControl 页面级样式UserControl.Resources style setter Property value. TargetType 应用的类型 使用 style static ...