From f2cfe2bb1a6d7da1c5dbf5cf506d6c45fc606870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC?= <2629351295@qq.com> Date: Mon, 26 Sep 2022 13:48:15 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 翁章彬 <2629351295@qq.com> --- ...7\347\250\213 \344\275\234\344\270\232.md" | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 "01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213 \344\275\234\344\270\232.md" diff --git "a/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213 \344\275\234\344\270\232.md" "b/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..52fa72c --- /dev/null +++ "b/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213 \344\275\234\344\270\232.md" @@ -0,0 +1,105 @@ +--1. 定义存储过程实现查询出账户余额最低的银行卡账户信息,显示银行卡号,姓名,账户余额 + +```sql +go +create proc p1 +as +select top 1 b.CardNo,(select RealName from AccountInfo a where a.AccountId = b.AccountId) as RealName,b.CardMoney from BankCard b +order by CardMoney +go +exec p1 +``` + +--2. 模拟银行卡存钱操作,传入银行卡号,存钱金额,实现存钱操作 + +```sql +go +create proc p2 +@CardNo varchar(30), +@CardMoney money +as +update BankCard set CardMoney += @CardMoney +insert into CardExchange values(@CardNo,@CardMoney,0,getdate()) +go +exec p2 '6225125478544587',2000 +select * from CardExchange c +select * from BankCard b +``` + +--3. 模拟银行卡取钱操作,传入银行卡号,取钱金额,实现取钱操作,取钱成功,返回1,取钱失败返回-1 + +```sql +go +create proc p3 +@CardNo varchar(30), +@CardMoney money +as +if @CardMoney<(select b.CardMoney from BankCard b where b.CardNo = @CardNo) + begin + update BankCard set CardMoney -= @CardMoney + insert into CardExchange values(@CardNo,0,@CardMoney,getdate()) + return 1 + end +else + begin + return -1 + end +go +declare @f int +exec @f = p3 '6225125478544587',100 +print @f +select * from CardExchange c +select * from BankCard b +``` + +--4. **查询出某时间段的银行存取款信息以及存款总金额**,取款总金额,传入开始时间,结束时间,显示存取款交易信息的同时,返回存款总金额,取款总金额。 + +```sql +go +create proc p4 +@k datetime, +@j datetime, +@MoneyInBank money output, +@MoneyOutBank money output +as +select * from CardExchange c +where c.ExchangeTime between @k and @j +select @MoneyInBank = sum(c.MoneyInBank) ,@MoneyOutBank = sum(c.MoneyOutBank) from CardExchange c +go +declare @MoneyInBank money +declare @MoneyOutBank money +exec p4 '2022-09-20','2022-09-27',@MoneyInBank output,@MoneyOutBank output +print '存款总金额'+ convert(varchar(20),@MoneyInBank) + '取款总金额'+ convert(varchar(20),@MoneyOutBank) +select c.MoneyOutBank from CardExchange c +select * from BankCard b +``` + +--5. **密码升级**,传入用户名和密码,如果用户名密码正确,并且密码长度<8,自动升级成8位密码 + +```sql +go +create proc p5 +@CardNo varchar(30), +@CardPwd varchar(10) +as +if @CardPwd=(select b.CardPwd from BankCard b where b.CardNo = @CardNo) + begin + while len(@CardPwd)<8 + begin + set @CardPwd += convert(varchar(10),round(rand()*9,0)) + end + update BankCard set CardPwd = @CardPwd where CardNo = @CardNo + end +else +begin + print '密码错误' +end +go +drop proc p5 +exec p5 '6225125478544587','123456' +select * from BankCard b +update BankCard set CardPwd = '123456' +``` + + +--(提示:随机生成 0-9 的整数: float(rand()*10)) rand():随机生成0.0-1.0的小数 float:向下取整) \ No newline at end of file -- Gitee From b5eaa6517929ceca18de3a1bc4dcb300a754b420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC?= <2629351295@qq.com> Date: Mon, 26 Sep 2022 13:48:28 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2001?= =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC/=E4=BD=9C=E4=B8=9A/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/.keep" diff --git "a/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/.keep" "b/01\347\277\201\347\253\240\345\275\254/\344\275\234\344\270\232/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee From eecc7c528a9de39727502717f771aead5ea4a7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC?= <2629351295@qq.com> Date: Mon, 26 Sep 2022 14:28:23 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 翁章彬 <2629351295@qq.com> --- ...30\345\202\250\350\277\207\347\250\213.md" | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 "01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213.md" diff --git "a/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213.md" "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213.md" new file mode 100644 index 0000000..d218b8a --- /dev/null +++ "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-09-26 \345\255\230\345\202\250\350\277\207\347\250\213.md" @@ -0,0 +1,84 @@ +# 定义存储过程 + +## 无参无返回值 + +### 没有输入参数,没有输出参数的存储过程 + +```sql +go +create proc 存储过程名 +as +语句 +go +exec 存储过程名 +``` + +## 有参无返回值 + +### 有输入参数,没有输出参数的存储过程 + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型 +as +语句 +go +exec 存储过程名 参数1,参数2 +``` + +## 有参有返回值 + +### 有输入参数,没有输出参数,但是有返回值的存储过程(返回值必须整数) + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型 +as +语句 +go +declare @f 数据类型 +exec @f = 存储过程名 参数1,参数2 +print @f +``` + +### 有输入参数,有输出参数的存储过程 + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型, +declare @变量3 数据类型 output, +declare @变量4 数据类型 output +as +语句 +go +declare @变量 数据类型 +declare @变量 数据类型 +exec 存储过程名 参数1,参数2,@变量 output,@变量 output +``` + +### 一个变量具备同时输入输出参数的存储过程 + +```sql +go +create proc 存储过程名 +declare @变量1 数据类型, +declare @变量2 数据类型, +as +语句 +go +declare @变量 数据类型 = 值 +exec 存储过程名 参数1,参数2,@变量 output +``` + +## **删除存储过程** + +```sql +drop procedure 存储过程名 +``` + -- Gitee