From 40fb8ba30f07d40cea1e94a8ae70d3340f1cfa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E5=88=A9=E7=BE=A4?= <2246026162@qq.com> Date: Fri, 7 Oct 2022 13:26:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E6=AC=A1=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-10-06\345\207\275\346\225\260.sql" | 129 ++++++++++++++++++ .../2022-10-06\345\207\275\346\225\260.md" | 54 ++++++++ 2 files changed, 183 insertions(+) create mode 100644 "32\351\231\206\345\210\251\347\276\244/\344\275\234\344\270\232/2022-10-06\345\207\275\346\225\260.sql" create mode 100644 "32\351\231\206\345\210\251\347\276\244/\347\254\224\350\256\260/2022-10-06\345\207\275\346\225\260.md" diff --git "a/32\351\231\206\345\210\251\347\276\244/\344\275\234\344\270\232/2022-10-06\345\207\275\346\225\260.sql" "b/32\351\231\206\345\210\251\347\276\244/\344\275\234\344\270\232/2022-10-06\345\207\275\346\225\260.sql" new file mode 100644 index 0000000..2bf91c0 --- /dev/null +++ "b/32\351\231\206\345\210\251\347\276\244/\344\275\234\344\270\232/2022-10-06\345\207\275\346\225\260.sql" @@ -0,0 +1,129 @@ + +--(1)编写一个函数求该银行的金额总和 +use BankTest +go +create function func_getBankMoneySum(@money money) +returns money +as +begin + declare @moneySum money = (select SUM(CardMoney) from BankCard) + return @moneySum +end +go +select dbo.func_getBankMoneySum as 和 + +--(2)传入账户编号,返回账户真实姓名 +select * from AccountInfo +go +create function func_getName(@id int) +returns varchar(50) +as +begin + declare @name varchar(50) = (select RealName from AccountInfo where AccountId = @id) + return @name +end +go +select dbo.func_getName(1) 姓名 + + +--(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 +select * from AccountInfo +select * from CardExchange +select * from CardTransfer + +--drop function getTransfer +go +create function getTransfer(@openTime smalldatetime) +returns @trans table( + RealName varchar(50), + CardNo varchar(50), + MoneyInBank money, + MoneyOutBank money, + ExchangeTime smalldatetime +) +as +begin + insert into @trans + select RealName,c.CardNo,MoneyInBank,MoneyOutBank,ExchangeTime from CardExchange c + join BankCard b on b.CardNo = c.CardNo + join AccountInfo a on a.AccountId = b.AccountId + where ExchangeTime = @openTime + group by RealName,c.CardNo,MoneyInBank,MoneyOutBank,ExchangeTime + return +end +go +select * from getTransfer('2022-09-20 18:36:00') + +--方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): + +--(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”, +-- 根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户", +--分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 +--方案一:直接在sql语句中使用case when +--方案二:将等级和状态用函数实现 + +select * from BankCard +-- drop function func_getMessage +go +create function func_getMessage(@state int,@money money) +returns @message table( + CardNo varchar(50), + CardState int, + state varchar(50), + CardMoney money, + lv varchar(50) + +) +as +begin + declare @sta varchar(50) + insert into @message + select + CardNo, + CardState, + case + when CardState = 1 then '正常' + when CardState = 2 then '挂失' + when CardState = 3 then '冻结' + else '注销' + end , + CardMoney , + case + when CardMoney < 300000 then '普通用户' + else 'VIP用户' + end + from BankCard + return +end +go +select * from func_getMessage(1,30) + + + +--(5)编写函数,根据出生日期求年龄,年龄求实岁,例如: +--​ 生日为2000-5-5,当前为2018-5-4,年龄为17岁 +--​ 生日为2000-5-5,当前为2018-5-6,年龄为18岁 +use sec +go +select * from Emp +go +-- drop function func_getAge +create function func_getAge(@birth smalldatetime) +returns int +as +begin +declare @age int +if(GETDATE() > @birth) +begin + set @age = year(getdate()) - year(@birth) +end +else +begin + set @age = year(getdate()) - year(@birth) - 1 +end + +return @age +end +go + +select dbo.func_getAge('2008-05-08 00:00:00') as 实岁 \ No newline at end of file diff --git "a/32\351\231\206\345\210\251\347\276\244/\347\254\224\350\256\260/2022-10-06\345\207\275\346\225\260.md" "b/32\351\231\206\345\210\251\347\276\244/\347\254\224\350\256\260/2022-10-06\345\207\275\346\225\260.md" new file mode 100644 index 0000000..8241470 --- /dev/null +++ "b/32\351\231\206\345\210\251\347\276\244/\347\254\224\350\256\260/2022-10-06\345\207\275\346\225\260.md" @@ -0,0 +1,54 @@ +## 函数 + +### 分类 + +(1)系统函数 + +(2)自定义函数(方法:将一个功能封装成可重用的函数)。 + +其中自定义函数又可以分为(1)标量值函数(返回单个值),(2)表值函数(返回查询结果) + +#### 标量值函数 + +``` +CREATE FUNCTION function_name(@parameter_name parameter_data_type) --(@参数名 参数的数据类型) +RETURNS date_type --返回返回值的数据类型 + +[AS] + +BEGIN + + function_body --函数体 + + RETURN 表达式; --必须要有的 + +END +``` + +#### 表值函数 + +``` +create function 名称 + +([{@参数名称 参数类型[=默认值]}[,n]]) + +returns @局部变量 table(参数名 参数类型) + +[with encryption] + +[as] + +begin + +函数体 + +return 函数返回值 + +end +``` + +删除自定义函数 + +``` +DROP function 函数名 +``` \ No newline at end of file -- Gitee