From 0b925934c944bba73f429c2ea5fb266ab2887023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Thu, 6 Oct 2022 09:17:30 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E4=B9=9D?= =?UTF-8?q?=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\344\271\235\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\344\271\235\346\254\241\344\275\234\344\270\232 /.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 99e80e4d31dcb43959183fa9e366ecc922491d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Thu, 6 Oct 2022 09:18:49 +0000 Subject: [PATCH 2/6] =?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\344\271\235\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\344\271\235\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\344\271\235\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 1350d320b7d8eba339ce97f439afbd5a40e8dd09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Thu, 6 Oct 2022 09:19:19 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94=2022?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../2022-10-06-\345\207\275\346\225\260.md" | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" new file mode 100644 index 0000000..78a1ac6 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" @@ -0,0 +1,106 @@ +## 回顾 + +事务-索引-视图-游标 + +- 访问或修改数据库内容的程序的执行称为事务 +- 事务的ACID特性:原子性、一致性、隔离性、持久性 +- 事务可以分为三种类型:显式事务、自动提交事务、隐式事务 +- 游标是一种数据对象,使用它可以按行而不是按集合操纵数据 +- 操纵游标的几个语名:DECLARE语句创建、OPEN语句打开、CLOSE语句关闭、DEALLOCATE可删除游标 +- FETCH可用于在游标中读取记录行 + +## 函数 + +函数分为(1)系统函数:,(2)自定义函数(方法:将一个功能封装成可重用的函数)。 + +其中自定义函数又可以分为(1)标量值函数(返回单个值),(2)表值函数(返回查询结果) + +本文主要介绍自定义函数的使用。 + +#### 标量值函数 + +语法结构: + +```sql +CREATE FUNCTION function_name(@parameter_name parameter_data_type) --(@参数名 参数的数据类型) +RETURNS date_type --返回返回值的数据类型 + +[AS] + +BEGIN + + function_body --函数体 + + RETURN 表达式; --必须要有的 + +END +``` + +定义函数要求实现: + +实现两个值的加和 + +使用函数求某门课的平均成绩 + + + +#### 表值函数 + +语法结构: + +```sql +create function 名称 + +([{@参数名称 参数类型[=默认值]}[,n]]) + +returns @局部变量 table(参数名 参数类型) + +[with encryption] + +[as] + +begin + +函数体 + +return 函数返回值 + +end +``` + + + +删除自定义函数 + +```sql +DROP function 函数名 +``` + + + + + + + + + +#### 练习 + +(1)编写一个函数求该银行的金额总和 + +(2)传入账户编号,返回账户真实姓名 + +(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 + +方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): + +(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +方案一:直接在sql语句中使用case when + +方案二:将等级和状态用函数实现 + +(5)编写函数,根据出生日期求年龄,年龄求实岁,例如: + +​ 生日为2000-5-5,当前为2018-5-4,年龄为17岁 +​ 生日为2000-5-5,当前为2018-5-6,年龄为18岁 -- Gitee From 89ffb0102fc8896589bce4ef71a994955034cf3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Fri, 7 Oct 2022 05:35:47 +0000 Subject: [PATCH 4/6] =?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\344\271\235\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\344\271\235\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\344\271\235\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 5ff0ece045d3d3ed99c9b87749eea2d1de338159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Fri, 7 Oct 2022 05:36:04 +0000 Subject: [PATCH 5/6] =?UTF-8?q?add=2022=20=E6=9D=A8=E5=87=8C=E7=BF=94/?= =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C=E4=B8=9A=20/=E4=BD=9C?= =?UTF-8?q?=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" | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\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\344\271\235\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\344\271\235\346\254\241\344\275\234\344\270\232 /\344\275\234\344\270\232/test.sql" new file mode 100644 index 0000000..df7573c --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\344\275\234\344\270\232/test.sql" @@ -0,0 +1,114 @@ +--(1)编写一个函数求该银行的金额总和 + create function summoney() + returns @sum1 table( + 金额总和 money + )as + begin + insert into @sum1 + select sum(cardmoney) from bankcard + return + end + select * from summoney() + ; +--(2)传入账户编号,返回账户真实姓名 + create function sename(@id int) + returns @realname table( + 真实姓名 varchar(50) + )as + begin + insert into @realname + select realname from AccountInfo where AccountId=@id + return + end + select * from sename(1) + ; +--(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 + create function betime(@begin smalldatetime,@end smalldatetime) + returns @betime table( + 真实姓名 varchar(50), + 卡号 varchar(50), + 存钱金额 money, + 取钱金额 money, + 交易时间 smalldatetime + )as + begin + insert into @betime + select na.RealName,na.CardNo,c.MoneyInBank,c.MoneyOutBank,c.ExchangeTime from CardExchange c inner join ( + select b.CardNo,a.RealName from AccountInfo a + inner join bankcard b on a.AccountId=b.AccountId + )na on na.CardNo =c.CardNo + where ExchangeTime>=@begin and ExchangeTime<=@end + return + end + select * from betime('2022-09-19 18:02:00','2022-09-19 18:05:00') +--方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): + +--(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +--方案一:直接在sql语句中使用case when + create function mes(@id int) + returns @message table( + 银行卡状态 varchar(50), + 银行卡等级 varchar(50) + )as + begin + insert into @message + select + case CardState + when 1 then '正常' + when 2 then '挂失' + when 3 then '冻结' + when 4 then '注销' + end 银行卡状态, + case + when cardmoney>300000 then 'VIP用户' + else '普通用户' + end 银行卡等级 + from BankCard where AccountId=@id + return + end + select * from mes(3) +--方案二:将等级和状态用函数实现 + +--(5)编写函数,根据出生日期求年龄,年龄求实岁,例如: + +--​ 生日为2000-5-5,当前为2018-5-4,年龄为17岁 +--​ 生日为2000-5-5,当前为2018-5-6,年龄为18岁 + create function age(@id int) + returns @age table( + 年龄 int + ) + as + begin + insert into @age + select + case + when month(GETDATE())>month(OpenTime) + then YEAR(GETDATE())-YEAR(OpenTime) + when month(GETDATE())day(OpenTime) + then YEAR(GETDATE())-YEAR(OpenTime) + when month(GETDATE())=month(OpenTime) and day(GETDATE()) Date: Fri, 7 Oct 2022 05:36:40 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=2022=20?= =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94/=E7=AC=AC=E4=B9=9D=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=20=20=E4=B8=BA=2022=20=E6=9D=A8=E5=87=8C?= =?UTF-8?q?=E7=BF=94/=E7=AC=AC=E5=8D=81=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 .../\344\275\234\344\270\232/.keep" | 0 .../\344\275\234\344\270\232/test.sql" | 0 .../\347\254\224\350\256\260/.keep" | 0 .../2022-10-06-\345\207\275\346\225\260.md" | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /.keep" => "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/.keep" (100%) rename "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\344\275\234\344\270\232/.keep" => "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" (100%) rename "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\344\275\234\344\270\232/test.sql" => "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" (100%) rename "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/.keep" => "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" (100%) rename "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" => "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" (100%) diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\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\346\254\241\344\275\234\344\270\232/.keep" similarity index 100% rename from "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /.keep" rename to "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\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\344\271\235\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\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" similarity index 100% rename from "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\344\275\234\344\270\232/.keep" rename to "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\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\344\271\235\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\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" similarity index 100% rename from "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\344\275\234\344\270\232/test.sql" rename to "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\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\344\271\235\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\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" similarity index 100% rename from "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/.keep" rename to "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\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\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" similarity index 100% rename from "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232 /\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" rename to "22 \346\235\250\345\207\214\347\277\224/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-06-\345\207\275\346\225\260.md" -- Gitee