From fe996f06b212d9736901d50479022d728eb2684a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AF=8C=E8=B4=B5?= <2380003452@qq.com> Date: Fri, 7 Oct 2022 05:12:55 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2036?= =?UTF-8?q?=E9=BB=84=E5=AF=8C=E8=B4=B5/=E6=96=B0=E5=BB=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=20(2)/10-6=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SQLQuery1.sql" | 137 ------------------ ...75\346\225\260\347\254\224\350\256\260.md" | 31 ---- 2 files changed, 168 deletions(-) delete mode 100644 "36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" delete mode 100644 "36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" diff --git "a/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" "b/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" deleted file mode 100644 index e9736e1..0000000 --- "a/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" +++ /dev/null @@ -1,137 +0,0 @@ -use BankTest -go - ---(1)编写一个函数求该银行的金额总和 -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 diff --git "a/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" "b/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" deleted file mode 100644 index f46a164..0000000 --- "a/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" +++ /dev/null @@ -1,31 +0,0 @@ -鍑芥暟鍒嗕负锛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 From 18380043088326ffe5eb97125fa98380e58a614a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AF=8C=E8=B4=B5?= <2380003452@qq.com> Date: Fri, 7 Oct 2022 05:13:12 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E9=BB=84=E5=AF=8C=E8=B4=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 榛勫瘜璐 <2380003452@qq.com> --- .../SQLQuery1.sql" | 137 ++++++++++++++++++ ...75\346\225\260\347\254\224\350\256\260.md" | 31 ++++ 2 files changed, 168 insertions(+) create mode 100644 "36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" create mode 100644 "36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" diff --git "a/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" "b/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000..e9736e1 --- /dev/null +++ "b/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,137 @@ +use BankTest +go + +--(1)编写一个函数求该银行的金额总和 +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 diff --git "a/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" "b/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" new file mode 100644 index 0000000..f46a164 --- /dev/null +++ "b/36\351\273\204\345\257\214\350\264\265/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271 (2)/10-6\344\275\234\344\270\232/10-6\345\207\275\346\225\260\347\254\224\350\256\260.md" @@ -0,0 +1,31 @@ +鍑芥暟鍒嗕负锛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