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) 创建数据表的时候 ...
随机推荐
- iOS UITableView 引起的崩溃问题
其实 UITableView 应该是在iOS开发中使用最频繁的一个控件,一次同事之间聊天玩笑的说“一个页面,要是没使用UITableView,就好像称不上是一个页面”.虽然是个最常见的控件,但是他的强 ...
- Android事件分发机制(上)
Android事件分发机制这个问题不止一个人问过我,每次我的回答都显得模拟两可,是因为自己一直对这个没有很好的理解,趁现在比较闲对这个做一点总结 举个例子: 你当前有一个非常简单的项目,只有一个Act ...
- EL表达式的使用
在JSP页面中嵌入了一部分Java代码,本以为Java代码中声明的变量可以通过${var}获得变量值输出,结果没有任何输出, EL表达式是用在对对象值.属性制的访问 ${user.id}.reques ...
- JQuery中$.ajax()方法参数都有哪些?
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- Apache Tomcat开机后台启动
作为软件开发人员,经常接触Tomcat,完成的项目,需要部署到服务器上的Tomcat,才能供其他人访问浏览. 因为存在以下问题,所以需要把Tomcat设置为后台自动启动: 1.服务器可能因环境故障面临 ...
- iOS保存cookie的方法
SURLRequest*request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/ ...
- 第二章 XHTML 基础
元素与标签术语,HTML/XHTMLXHTML之间的联系区别在XHTML中,所有元素之间必须完成正确的嵌套,元素必须是闭合的,必须小写.必须有个跟元素HTML. 标题标<h1>语法:< ...
- 用递归法判断字符串A中包含多少个字符串B
string类提供了判断字符串B在字符串A中首次(或最后)出现的Index的方法,但有时候需要判断B在A中出现了多少次. 为此想了一个算法. public static void CountIndex ...
- Container With Most Water -- LeetCode 11
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- fatal: Could not read from remote repository.的解决办法
1. git remote –v查看远端地址或者查看配置 git config –list 2. git status 3. git add . git status git commit -m “本 ...