From bf556573e49dabd85938f25d3b99126f20896e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= <932306904@qq.com> Date: Mon, 10 Oct 2022 13:27:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=B4=E6=AC=A3=E7=87=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232.md" | 81 +++++++++++++++++++ .../2022-10-10\345\202\250\345\255\230.md" | 35 ++++++++ 2 files changed, 116 insertions(+) create mode 100644 "31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\344\275\234\344\270\232.md" create mode 100644 "31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-10\345\202\250\345\255\230.md" diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\344\275\234\344\270\232.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\344\275\234\344\270\232.md" new file mode 100644 index 0000000..0925737 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\344\275\234\344\270\232.md" @@ -0,0 +1,81 @@ +``` +定义存储过程实现查询出账户余额最低的银行卡账户信息,显示银行卡号,姓名,账户余额 +create proc proc_GetBankMessage +as +select a.RealName,b.CardNo,b.CardMoney from AccountInfo a inner join BankCard b on a.AccountId = b.AccountId +where b.CardMoney = (select min(CardMoney) from BankCard) +go +exec proc_GetBankMessage +go +--模拟银行卡存钱操作,传入银行卡号,存钱金额,实现存钱操作 +create proc proc_InMoney +@cardNo varchar(20), +@InMoney money +as +begin + update BankCard set CardMoney += @InMoney where CardNo = @cardNo + insert into CardExchange values(@cardNo,@InMoney,0,getdate()) + +end +go +exec proc_InMoney '6225547854125656',5000 +go + +--模拟银行卡取钱操作,传入银行卡号,取钱金额,实现取钱操作,取钱成功,返回1,取钱失败返回-1 +create proc proc_OutMoney +@cardNo varchar(20), +@OutMoney money +as +begin + declare @code int + declare @CardMoney money = (select CardMoney from BankCard where CardNo = @cardNo) + if(@CardMoney - @OutMoney > 0) + begin + update BankCard set CardMoney -= @CardMoney where CardNo = @cardNo + insert into CardExchange values (@cardNo,0,@OutMoney,getdate()) + return 1 + end + return -1 +end +go +declare @code int + exec @code = proc_OutMoney '6225547858741263',7000 + print @code + go + --**查询出某时间段的银行存取款信息以及存款总金额**,取款总金额,传入开始时间,结束时间, + --显示存取款交易信息的同时,返回存款总金额,取款总金额。 + create proc proc_GetChargeMessage + @Start_time smalldatetime, + @End_time smalldatetime, + @SumInMoney money output, + @SumOutMoney money output + as + begin + select @SumInMoney = sum(MoneyInBank),@SumOutMoney = sum(MoneyOutBank) from CardExchange + where ExchangeTime between @Start_time and @End_time + end + go +declare @Start_time smalldatetime = '2022-09-05 ', @End_time smalldatetime = '2022-09-19' +declare @SumInMoney money , @SumOutMoney money +exec proc_GetChargeMessage @Start_time,@End_time,@SumInMoney output,@SumOutMoney output +select @SumInMoney 存款总金额,@SumOutMoney 取款总金额 +go +--**密码升级**,传入用户名和密码,如果用户名密码正确,并且密码长度<8,自动升级成8位密码 +--(提示:随机生成 0-9 的整数: floor(rand()*10)) rand():随机生成0.0-1.0的小数 floor:向下取整) +create proc proc_PassWordUp +@userName varchar(10),@passWord varchar(10) output +as +begin + if(@userName = '呼呼' and @passWord = '123456' and len(@passWord)<8) + begin + set @passWord = Convert(decimal(8,0),floor(rand()*100000000)) + end +end +go +drop proc proc_PassWordUp +declare @userName varchar(10) = '呼呼' +declare @passWord varchar(10) = '123456' + +exec proc_PassWordUp @userName,@passWord output +select @passWord +``` \ No newline at end of file diff --git "a/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-10\345\202\250\345\255\230.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-10\345\202\250\345\255\230.md" new file mode 100644 index 0000000..6dedfe0 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-10\345\202\250\345\255\230.md" @@ -0,0 +1,35 @@ +分类 (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