From 49065afe4d48e57a3f259b7b3041f00defd25a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Sun, 9 Oct 2022 20:30:29 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E5=8D=81?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 8037f8bbc4f18d25ab45bae5ea91d220986d72d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Sun, 9 Oct 2022 20:30:38 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 52cab5e6265ba637784e5b76e884a35b4aed7138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Sun, 9 Oct 2022 20:30:45 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BD=9C=E4=B8=9A?= 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(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 33db232cd113ff1037a96b2b147400ecb6bd55af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Sun, 9 Oct 2022 20:31:07 +0000 Subject: [PATCH 4/5] =?UTF-8?q?add=2022=20=E6=9D=A8=E5=87=8C=E7=BF=94/?= =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A/?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A/test.sql.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../\344\275\234\344\270\232/test.sql" | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" new file mode 100644 index 0000000..6573624 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" @@ -0,0 +1,128 @@ + +use BankTest +go + +--1. 定义存储过程实现查询出账户余额最低的银行卡账户信息,显示银行卡号,姓名,账户余额 +select * from BankCard +go +-- drop proc proc_getMinMoney +create proc proc_getMinMoney +@minMoney money output +as +begin + select @minMoney = MIN(CardMoney) from BankCard + select CardNo,RealName,CardMoney from BankCard b + join AccountInfo a on a.AccountId = b.AccountId + where CardMoney = @minMoney +end +go + +declare @minMoney money +exec proc_getMinMoney @minMoney output + + + +--2. 模拟银行卡存钱操作,传入银行卡号,存钱金额,实现存钱操作 +select * from CardExchange +go +create proc proc_getInMoney +@cardNo varchar(50), +@inMoney money +as +begin + insert into CardExchange values(@cardNo,@inMoney,0,GETDATE()) + +end +go +-- +declare @cardNo varchar(50) = '6225125478544587', + @inMoney money = 8848 +exec proc_getInMoney @cardNo,@inMoney +select * from CardExchange + + + +--3. 模拟银行卡取钱操作,传入银行卡号,取钱金额,实现取钱操作,取钱成功,返回1,取钱失败返回-1 +select * from BankCard +go +-- drop proc proc_outMoney + +create proc proc_outMoney +@cardNum varchar(50), +@outMoney money, +@backNum int output +as +begin + select @backNum = (case when CardMoney >= @outMoney then 1 else -1 end) from BankCard + where CardNo = @cardNum + update BankCard set CardMoney -= @outMoney where CardNo = @cardNum +end +go +--执行 +declare @cardNum varchar(50) = '6225547854125656', +@outMoney money = 1000, +@backNum int +exec proc_outMoney @cardNum,@outMoney,@backNum output +select @backNum +select * from BankCard + + +--4. 查询出某时间段的银行存取款信息以及存款总金额**,取款总金额, +--传入开始时间,结束时间,显示存取款交易信息的同时,返回存款总金额,取款总金额。 +select * from CardExchange --交易记录 + +-- drop proc proc_getBankMessage +go +create proc proc_getBankMessage +@openTime smalldatetime, +@overTime smalldatetime, +@InSum money output, +@OutSum money output +as +begin + select @InSum = SUM(MoneyInBank),@OutSum = SUM(MoneyOutBank) from CardExchange + where ExchangeTime between @openTime and @overTime +end +go + +declare @openTime smalldatetime = '2022-09-07 14:30:00', +@overTime smalldatetime = '2022-09-27 08:19:00', +@InSum money,@OutSum money + +exec proc_getBankMessage @openTime,@overTime,@InSum output,@OutSum output +select @InSum 存款总额,@OutSum 取款总额 + + +--5. **密码升级**,传入用户名和密码,如果用户名密码正确,并且密码长度<8,自动升级成8位密码 +--(提示:随机生成 0-9 的整数: float(rand()*10)) rand():随机生成0.0-1.0的小数 float:向下取整) + +-- drop proc proc_pwd +-- update BankCard set CardPwd = '123456' where AccountId = 1 +go +create proc proc_pwd +@cardNo varchar(50), +@pwd varchar(50) +as +begin + if((select CardNo from BankCard where CardNo = @cardNo) != ' ' + and @pwd = (select CardPwd from BankCard where CardNo = @cardNo and @pwd = CardPwd)) + begin + if(len(@pwd) < 8) + begin + update BankCard set CardPwd = CONCAT(ROUND(99999+RAND()*99999,0),ROUND(RAND()*9,0),ROUND(RAND()*9,0)) where CardNo = @cardNo + end + end + else + begin + print '错误了X_X' + end +end +go +-- 执行 +declare @cardNo varchar(50) = '6225125478544587',@pwd varchar(50) = '123456' +exec proc_pwd @cardNo,@pwd +select * from BankCard + + + + -- Gitee From 1fda81430d3e28943196c3316d801071ec127c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Sun, 9 Oct 2022 20:31:49 +0000 Subject: [PATCH 5/5] =?UTF-8?q?add=2022=20=E6=9D=A8=E5=87=8C=E7=BF=94/?= =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A/?= =?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: 杨凌翔 <963304364@qq.com> --- ...30\345\202\250\350\277\207\347\250\213.md" | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-10-\345\255\230\345\202\250\350\277\207\347\250\213.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-10-\345\255\230\345\202\250\350\277\207\347\250\213.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-10-\345\255\230\345\202\250\350\277\207\347\250\213.md" new file mode 100644 index 0000000..7971a22 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-10-\345\255\230\345\202\250\350\277\207\347\250\213.md" @@ -0,0 +1,144 @@ + + +## -- 10.7 存储过程(Stored Procedure) + + + +存储过程是**预编译**SQL语句集合,这些语句存储在一个名称(存储过程的名称)下并作为单元来处理。存储过程代替了传统的逐条执行SQL语句的方式,一个存储过程中可以包含查询、插入、删除、更新等操纵的一系列SQL语句,当这个存储过程被调用执行时,这些操作也会同时执行。 + +封装好 --> 调用 + +#### 存储过程的分类 + +##### 系统存储过程 + + 系统存储过程是用来管理SQL Server与显示有关数据库和用户的信息的存储过程。 + +```sql +sp_databases 列出服务上的所有数据库 +sp_helpdb --报告有关指定数据库或所有数据库的信息 +sp_renamedb 更改数据库的名称 +sp_tables --返回当前环境下可查询的对象的列表 +sp_columns 返回某个表列的信息 +sp_help --返回某个表的所有信息 +sp_helpconstraint 查看某个表的约束 +sp_helpindex --查看某个表的索引 +sp_stored_procedures 列出当前环境中的所有存储过程 +sp_password --添加或修改登录账户的密码 +sp_rename 重命名存储过程 +sp_helptext 显示默认值,未加密的存储过程、用户定义的存储过程、触发器或视图的实际文本。 +``` + + + +##### 自定义存储过程 + +**创建存储过程** + +1.没有输入参数,没有输出参数的存储过程。 + +```sql +create proc <存储过程名称> +as + +go +``` + +2.有输入参数,没有输出参数的存储过程 + +```sql +create proc <存储过程名称> +<变量1> <数据类型> +<变量2> <数据类型> +... +as + +go +``` + +3.有输入参数,没有输出参数,但是有返回值的存储过程(返回值必须整数)。 + +```sql +create proc <存储过程名称> +<变量1> <数据类型> +<变量2> <数据类型> +... +as + +return 整数 +go +``` + +4.有输入参数,有输出参数的存储过程 & 一个变量具备同时输入输出参数的存储过程 + +```sql +create proc <存储过程名称> +<变量1> <数据类型> output +<变量2> <数据类型> output +... +as + +return 整数 +go +``` + + + +**执行存储过程** + +```sql +--无参 +exec <存储过程名称> +--带参 +exec <存储过程名称> <形参1>,<形参2>,... +--带参带返回值 +declare @变量 +exec @变量 = <存储过程名称> <形参1>,<形参2>,... +--有输入参数,有输出参数的存储过程 +declare @变量 +exec <存储过程名称> <形参1>,<形参2>,@变量 output +--一个变量同时具备输入输出功能 +declare @变量 <数据类型> = 值 +exec <存储过程名称> <形参1>,<形参2>,@变量 output +``` + + + +**删除存储过程** + +```sql +drop procedure <存储过程名称> +``` + + + +#### 存储过程的优点 + +允许模块化程序设计 + +执行速度更快 + +减少网络流通量 + +提高系统安全性 + + + +#### 存储过程与函数的区别 + +1.储存过程可以有返回值也可以无返回值。函数必须有返回值。 + +2.存储过程的实现比较复杂,而函数的实现比较有针对性。 + +3.储存过程可以输入输出参数,而函数只可以输入参数。 + +4.过程允许在其中选择以及DML语句,而函数只可以在其中select语句。 + +5.可以在存储过程中调用函数,不可以在函数中调用存储过程。 + +6.函数可以作为条件在语句中使用,但是存储过程不可以。 + + + + + -- Gitee