From 3fd074a9bc23b207cadb5fe2eb853643a47587e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=B5=A9=E6=95=8F?= <3127104562@qq.com> Date: Fri, 7 Oct 2022 13:07:01 +0800 Subject: [PATCH] 10-6 --- ...44\270\2329\346\234\21018\345\217\267.sql" | 0 .../9.18\347\254\224\350\256\260/DBTEST.sql" | 0 .../10-6\345\207\275\346\225\260.md" | 67 ++++++++++ .../10-6\345\207\275\346\225\260.sql" | 122 ++++++++++++++++++ 4 files changed, 189 insertions(+) rename "\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\344\275\234\344\270\232/\344\275\234\344\270\2329\346\234\21018\345\217\267.sql" => "27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\344\275\234\344\270\232/\344\275\234\344\270\2329\346\234\21018\345\217\267.sql" (100%) rename "\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\347\254\224\350\256\260/DBTEST.sql" => "27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\347\254\224\350\256\260/DBTEST.sql" (100%) create mode 100644 "27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.md" create mode 100644 "27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.sql" diff --git "a/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\344\275\234\344\270\232/\344\275\234\344\270\2329\346\234\21018\345\217\267.sql" "b/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\344\275\234\344\270\232/\344\275\234\344\270\2329\346\234\21018\345\217\267.sql" similarity index 100% rename from "\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\344\275\234\344\270\232/\344\275\234\344\270\2329\346\234\21018\345\217\267.sql" rename to "27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\344\275\234\344\270\232/\344\275\234\344\270\2329\346\234\21018\345\217\267.sql" diff --git "a/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\347\254\224\350\256\260/DBTEST.sql" "b/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\347\254\224\350\256\260/DBTEST.sql" similarity index 100% rename from "\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\347\254\224\350\256\260/DBTEST.sql" rename to "27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\344\272\214\346\254\241\347\254\224\350\256\260/9.18\347\254\224\350\256\260/DBTEST.sql" diff --git "a/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.md" "b/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.md" new file mode 100644 index 0000000..e33a402 --- /dev/null +++ "b/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.md" @@ -0,0 +1,67 @@ +## 函数 + +函数分为(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 函数名 +``` + diff --git "a/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.sql" "b/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.sql" new file mode 100644 index 0000000..c368cd8 --- /dev/null +++ "b/27\345\217\267\350\265\265\346\265\251\346\225\217/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/10-6\345\207\275\346\225\260.sql" @@ -0,0 +1,122 @@ +select * from AccountInfo --个人信息 +select * from BankCard --银行卡信息 +select * from CardStateChange --银行卡状态更改表 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 + +--(1)编写一个函数求该银行的金额总和 +go + create function func_GetSumm() +returns @su table( + CardMoney money +) +as +begin + insert into @su + select sum(CardMoney) from BankCard + return +end +go +select * from func_GetSumm() + + +--(2)传入账户编号,返回账户真实姓名 + go + create function xxx(@accountId int) +returns @info table( + RealName varchar(20) +) +as +begin + insert into @info + select RealName from AccountInfo where AccountId = @accountId + return + end +go + select * from xxx(1) + + + +--(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 + +--方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): +go + create function jiao(@begin smalldatetime,@end smalldatetime) + returns @btime table( + RealName varchar(20), + CardNo varchar(20), + MoneyIn money, + MoneyOut money, + CardTime smalldatetime + ) +as +begin + insert into @btime + 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 + go + select * from jiao('2022-09-19 18:02:00','2022-09-19 18:05:00') + + +--(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +--方案一:直接在sql语句中使用case when +go +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 +go + select * from mes(3) + +--方案二:将等级和状态用函数实现 + +--(5)编写函数,根据出生日期求年龄,年龄求实岁,例如: + +​ -- 生日为2000-5-5,当前为2018-5-4,年龄为17岁 +​ -- 生日为2000-5-5,当前为2018-5-6,年龄为18岁 +go +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())