From 8769b1f23077223a2aba282d6a3dbdb5a4c97727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Fri, 7 Oct 2022 05:51:29 +0000 Subject: [PATCH 1/2] =?UTF-8?q?10.6=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 郑文源 <> --- .../10.6\344\275\234\344\270\232.txt" | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 "13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/10.6\344\275\234\344\270\232.txt" diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/10.6\344\275\234\344\270\232.txt" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/10.6\344\275\234\344\270\232.txt" new file mode 100644 index 0000000..5d6cadd --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/10.6\344\275\234\344\270\232.txt" @@ -0,0 +1,135 @@ +use BankTest +go +create function function_sum() +returns money +as +begin +declare @Moneysum money =(select sum(CardMoney) from BankCard) + +return @Moneysum +end +go +select dbo.function_sum() as 和 +drop function function_sum + +--(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)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 + +--方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): +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') + +--(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +--方案一:直接在sql语句中使用case when + +--方案二:将等级和状态用函数实现 +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)编写函数,根据出生日期求年龄,年龄求实岁,例如: +create table Emp +( + EmpId int primary key identity(1,2), --自动编号 + empName varchar(20), --姓名 + empSex varchar(4), --性别 + empBirth smalldatetime --生日 +) +insert into Emp(empName,empSex,empBirth) values('刘备','男','2008-5-8') +insert into Emp(empName,empSex,empBirth) values('关羽','男','1998-10-10') +insert into Emp(empName,empSex,empBirth) values('张飞','男','1999-7-5') +insert into Emp(empName,empSex,empBirth) values('赵云','男','2003-12-12') +insert into Emp(empName,empSex,empBirth) values('马超','男','2003-1-5') +insert into Emp(empName,empSex,empBirth) values('黄忠','男','1988-8-4') +insert into Emp(empName,empSex,empBirth) values('魏延','男','1998-5-2') +insert into Emp(empName,empSex,empBirth) values('简雍','男','1992-2-20') +insert into Emp(empName,empSex,empBirth) values('诸葛亮','男','1993-3-1') +insert into Emp(empName,empSex,empBirth) values('徐庶','男','1994-8-5') +--? 生日为2000-5-5,当前为2018-5-4,年龄为17岁 +--? 生日为2000-5-5,当前为2018-5-6,年龄为18岁 +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 -- Gitee From 17c9506011aa98240690d88cf4c089d5796668e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=96=87=E6=BA=90?= <10033692+zhengwenyuan@user.noreply.gitee.com> Date: Fri, 7 Oct 2022 05:51:49 +0000 Subject: [PATCH 2/2] =?UTF-8?q?10.6=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 郑文源 <> --- .../10.6\347\254\224\350\256\260.txt" | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 "13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/10.6\347\254\224\350\256\260.txt" diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/10.6\347\254\224\350\256\260.txt" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/10.6\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..c51a1a7 --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/10.6\347\254\224\350\256\260.txt" @@ -0,0 +1,7 @@ +函数分为(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 函数名 总结:自定义函数 1.标量值函数: 1.returns 数据类型 2.return 标量(某个值) 2.多语句表值函数 1.returns 表名 table(字段 数据类型) 2.return \ No newline at end of file -- Gitee