diff --git "a/06\351\231\210\346\242\205\351\246\231/\344\275\234\344\270\232/2022.10.06\344\275\234\344\270\232.sql" "b/06\351\231\210\346\242\205\351\246\231/\344\275\234\344\270\232/2022.10.06\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bc6eae692e09b53abc51959bef85e9d503ba8848 --- /dev/null +++ "b/06\351\231\210\346\242\205\351\246\231/\344\275\234\344\270\232/2022.10.06\344\275\234\344\270\232.sql" @@ -0,0 +1,130 @@ + +--(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/06\351\231\210\346\242\205\351\246\231/\347\254\224\350\256\260/2022.10.06\347\263\273\347\273\237\345\207\275\346\225\260.md" "b/06\351\231\210\346\242\205\351\246\231/\347\254\224\350\256\260/2022.10.06\347\263\273\347\273\237\345\207\275\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..bf21bfa290cafd5c2e8a00c3ed25c7c93da61084 --- /dev/null +++ "b/06\351\231\210\346\242\205\351\246\231/\347\254\224\350\256\260/2022.10.06\347\263\273\347\273\237\345\207\275\346\225\260.md" @@ -0,0 +1,50 @@ +锛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