diff --git "a/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.10.6.sql" "b/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.10.6.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0cfd6227ccc1024882da73b4500ce1c445aa07d6 --- /dev/null +++ "b/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.10.6.sql" @@ -0,0 +1,164 @@ +--(1)编写一个函数求该银行的金额总和 +use BankTest +go +create function func_GetBankMoneyByAccId() +returns money +as +begin + declare @SumBankMoney money + select @SumBankMoney =(select sum(CardMoney ) from BankCard) + return @SumBankMoney +end +go + + +select dbo.func_GetBankMoneyByAccId() as 银行金额总和 + + +--(2)传入账户编号,返回账户真实姓名 +go +create function func_GetInfoByAcco_Id(@id int) +returns varchar(40) +as +begin + declare @name varchar(40) + select @name = (select RealName from AccountInfo where AccountId = @id) + return @name +end +go + +select dbo.func_GetInfoByAcco_Id( 2 ) as 姓名 + + +--(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 + +--方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): + +go +create function func_GetCardExchangeByOpenTimeAndEndTime(@OpenTime smalldatetime,@EndTime smalldatetime) +returns @ExchangTable table( + Name varchar(30), + CardNo varchar(50), + MoneyInBank money, + MoneyOutBank money, + ExchangeTime smalldatetime +) +as +begin + insert into @ExchangTable + select A.RealName,CE.CardNo,CE.MoneyInBank,CE.MoneyOutBank,CE.ExchangeTime from CardExchange CE + join BankCard B on B.CardNo = CE.CardNo + join AccountInfo A on A.AccountId = B.AccountId + where A.OpenTime = @OpenTime and CE.ExchangeTime = @EndTime + return +end +go + +select * from func_GetCardExchangeByOpenTimeAndEndTime('2022-09-19 22:37:00','2022-09-19 22:37:00') + + + + +--(4)查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为 +--“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +--方案一:直接在sql语句中使用case when +select B.CardNo,AccountCode,RealName,CardMoney, +( + case when CardMoney<300000 then '普通用户' + else 'VIP用户' + end +)用户等级, +( + case when CardState=1 then '正常' + when CardState=2 then '挂失' + when CardState=3 then '冻结' else '注销' + end +)银行卡状态 +from BankCard B +join AccountInfo A on A.AccountId = B.AccountId + + +--方案二:将等级和状态用函数实现 +--查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,根据银行卡余额显示银行卡等级 30万以下为 +--“普通用户”,30万及以上为"VIP用户",分别显示卡号,身份证,姓名,余额,用户等级,银行卡状态。 +go +create function fun_GetBankInfoByCardNo(@CardNo varchar(30)) +returns @BankInfo table( + CardNo varchar(30), + AccountCode varchar(30), + Name varchar(30), + CardMoney money, + UserLevel varchar(30), + BankCardStatus varchar(30) +) +as +begin + insert into @BankInfo + select B.CardNo,AccountCode,RealName,CardMoney, + ( + case when CardMoney<300000 then '普通用户' + else 'VIP用户' + end + )UserLevel, + ( + case when CardState=1 then '正常' + when CardState=2 then '挂失' + when CardState=3 then '冻结' else '注销' + end + )BankCardStatus + from BankCard B + join AccountInfo A on A.AccountId = B.AccountId + where CardNo = @CardNo + return +end +go + +select * from fun_GetBankInfoByCardNo('6225547854125656') + + +select * from AccountInfo +select * from CardExchange +select * from BankCard + + +--(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岁 + +select * from Emp +go +create function fun_GetAgeByBirth(@id int,@NowTime datetime) +returns int +as +begin + declare @BirthTime datetime + declare @age int + select @BirthTime = (select empBirth from Emp where EmpId = @id) + select @age = DATEDIFF(YY,@BirthTime,@NowTime) + select @age = case when DATEDIFF(DD,DATEADD(YY,@age,@BirthTime),@NowTime)<0 then @age-1 else @age end + return @age +end +go + +select dbo.fun_GetAgeByBirth(9,GETDATE()) as 年龄 \ No newline at end of file diff --git "a/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.10.7.sql" "b/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.10.7.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ab76422284b1f1841eaefde23a26e378a7655dfb --- /dev/null +++ "b/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.10.7.sql" @@ -0,0 +1,88 @@ +--1. 洢ʵֲѯ˻͵п˻Ϣ,ʾпţ˻ +create proc proc_MinMoneyCard +as +begin +-- +select top 1 CardNo п,RealName ,CardMoney +from BankCard inner join AccountInfo on BankCard.AccountId = AccountInfo.AccountId +order by CardMoney asc +end +go +--2. ģпǮпţǮʵִǮ +create proc proc_CunQian +@CardNo varchar(30), +@MoneyInBank money +as +update BankCard set CardMoney = CardMoney + @MoneyInBank where CardNo = @CardNo +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values(@CardNo,@MoneyInBank,0,GETDATE()) +go +-- +exec proc_CunQian '6225125478544587',3000 + +--3. ģпȡǮпţȡǮʵȡǮȡǮɹ1ȡǮʧܷ-1 +Create proc proc_Quqian + @CardNo carchar(30) + @money money +As + -- ޸IJ + Update BankCard set CardMoney = CardMoney - @money + Where CardNo = @CardNo + -- д򷵻-1 + -- ڴ洢Уreturn д붼ִ + If @@ERROR <>0 + Return -1 + Insert into CardExchange(CardNo, MoneyInBank, MoneyOutBank, ExchangeTime) + Values(@CardNo, 0, @money, getdate()) + Return 1 +Go +--4. **ѯijʱεдȡϢԼܽ**ȡܽ +--뿪ʼʱ䣬ʱ䣬ʾȡϢͬʱشܽȡܽ +create proc GetMoneyByTime +@startTime smalldatetime, +@endTime smalldatetime, +@TotalSaveMoney money output, +@TotalWithdrawMoney money output +as +begin + select @TotalSaveMoney = sum(MoneyInBank),@TotalWithdrawMoney=sum(MoneyOutBank) from cardExchange + where ExchangeTime between @startTime and @endTime +end + +-- +declare @startTime smalldatetime = '2022-09-07',@endTime smalldatetime = '2022-09-18' +declare @TotalSaveMoney money,@TotalWithdrawMoney money +exec GetMoneyByTime @startTime,@endTime,@TotalSaveMoney output , @TotalWithdrawMoney output +select @TotalSaveMoney ܶ,@TotalWithdrawMoney ȡܶ +--5. ****û룬ûȷ볤<8Զ8λ +--(ʾ 0-9 float(rand()*10)) rand():0.0-1.0С float:ȡ +select FLOOR(RAND()*10) --0-9֮ +create proc procPwdUpgrade +@cardno nvarchar(20), +@pwd nvarchar(20) output +as + if not exists(select * from BankCard where CardNo=@cardno and CardPwd=@pwd) + set @pwd = '' + else + begin + if len(@pwd) < 8 + begin + declare @len int = 8- len(@pwd) + declare @i int = 1 + while @i <= @len + begin + + set @pwd = @pwd + cast(FLOOR(RAND()*10) as varchar(1)) + set @i = @i+1 + end + update BankCard set CardPwd = @pwd where CardNo=@cardno + end + end +go + + + +--ãʼֵ +declare @pwd nvarchar(20) = '123456' +exec procPwdUpgrade '6225547854125656',@pwd output +select @pwd \ No newline at end of file diff --git "a/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.30.sql" "b/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.30.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ecf149e02e18bfe597266fa40308f0bcb729a1d4 --- /dev/null +++ "b/10\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.30.sql" @@ -0,0 +1,116 @@ +--3ʹαʵּн +use test_trigger +select * from People + + + --- 1.α + -- 1.1α (scroll) +declare cur_UpdateSalaryById cursor scroll +for select PeopleId from People + + -- 2.α +open cur_UpdateSalaryById + -- 3 ʹα + --αŻصֵ żнֵ +declare @id int +declare @money money = 1000 + -- ע ʹñ洢αֵĸʽ fetch ... from α into +fetch first from cur_UpdateSalaryById into @id + --@@fetch_status = 0 : һ -1: ȡʧ -2:ȡհ + -- ÿ˶н1000 +while(@@fetch_status = 0) + begin + update People set PeopleSalary += @money where PeopleId = @id + print cast(@id as varchar(12)) + char(9) + cast(@money as varchar(20)) + fetch next from cur_UpdateSalaryById into @id + end + + -- 4.رα +close cur_UpdateSalaryById + + -- 5.ͷű +deallocate cur_UpdateSalaryById + +--4:ʹαɾԱ marketֶΣ +go + select * from People + alter table People add market varchar(20) +update People set market = '1000' where PeopleId = 1 +update People set market = '1500' where PeopleId = 2 +update People set market = '2000' where PeopleId = 6 + + +declare cur_DropMarketById cursor scroll +for select PeopleId from People + +open cur_DropMarketById + +declare @id int +declare @Mk varchar(20) = '' +fetch first from cur_DropMarketById into @id + +while(@@FETCH_STATUS = 0) + begin + set @Mk = (select market from People where PeopleId = @id) + if(@Mk <= 1000) + delete from People where PeopleId = @id + else + fetch next from cur_DropMarketById into @id + end +close cur_DropMarketById +deallocate cur_DropMarketById + +-- +-- **************************************************************************************** +go +create table A( + id int primary key, + Province varchar(20), + City varchar(20) +) + +create table B( + id int primary key, + Province varchar(20), + City varchar(20) +) + +insert into A values(1,'','') +insert into A values(2,'㶫','') +insert into A values(3,'','') +insert into A values(4,'','') + +insert into B values(1,'','ϲ') +insert into B values(3,'','Ͼ') +insert into B values(5,'','人') +insert into B values(7,'','ɳ') + +--Ҫʹα ʵ:ABIDֵֶͬôBеʡݣ ޸ijAеijһ£ +select * from A +select * from B + + +declare cur_tempid cursor scroll +for select A.* from A join B on B.id = A.id + +open cur_tempid + + +declare @id int +declare @Province varchar(20) +declare @City varchar(20) + +fetch first from cur_tempid into @id,@Province,@City + + +while(@@FETCH_STATUS = 0) + begin + update B set Province = @Province where id = @id + update B set City = @City where id = @id + fetch next from cur_tempid into @id,@Province,@City + end + + +close cur_tempid + +deallocate cur_tempid diff --git "a/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.10.6\345\207\275\346\225\260.md" "b/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.10.6\345\207\275\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..4c01b0528d68bf5fbd7a0fb3cd1fbc879b3266d7 --- /dev/null +++ "b/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.10.6\345\207\275\346\225\260.md" @@ -0,0 +1,109 @@ +函数分为(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)编写一个函数求该银行的金额总和 +use BankTest +go +create function func_GetBankMoneyByAccId() +returns money +as +begin + declare @SumBankMoney money + select @SumBankMoney =(select sum(CardMoney ) from BankCard) + return @SumBankMoney +end +go + + +select dbo.func_GetBankMoneyByAccId() as 银行金额总和 + + +--(2)传入账户编号,返回账户真实姓名 +go +create function func_GetInfoByAcco_Id(@id int) +returns varchar(40) +as +begin + declare @name varchar(40) + select @name = (select RealName from AccountInfo where AccountId = @id) + return @name +end +go + +select dbo.func_GetInfoByAcco_Id( 2 ) as 姓名 + + +--(3)传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含 真实姓名,卡号,存钱金额,取钱金额,交易时间。 + +--方案一(逻辑复杂,函数内容除了返回结果的sql语句还有其他内容,例如定义变量等): + +go +create function func_GetCardExchangeByOpenTimeAndEndTime(@OpenTime smalldatetime,@EndTime smalldatetime) +returns @ExchangTable table( + Name varchar(30), + CardNo varchar(50), + MoneyInBank money, + MoneyOutBank money, + ExchangeTime smalldatetime +) +as +begin + insert into @ExchangTable + select A.RealName,CE.CardNo,CE.MoneyInBank,CE.MoneyOutBank,CE.ExchangeTime from CardExchange CE + join BankCard B on B.CardNo = CE.CardNo + join AccountInfo A on A.AccountId = B.AccountId + where A.OpenTime = @OpenTime and CE.ExchangeTime = @EndTime + return +end +go + +select * from func_GetCardExchangeByOpenTimeAndEndTime('2022-09-19 22:37:00','2022-09-19 22:37:00') \ No newline at end of file diff --git "a/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.10.7\345\255\230\345\202\250\350\277\207\347\250\213.md" "b/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.10.7\345\255\230\345\202\250\350\277\207\347\250\213.md" new file mode 100644 index 0000000000000000000000000000000000000000..a8ffd539decb47a4fb1561743fffe3b8baa631f0 --- /dev/null +++ "b/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.10.7\345\255\230\345\202\250\350\277\207\347\250\213.md" @@ -0,0 +1,163 @@ +--execute:执行 +exec sp_databases --查看所有数据库 + +exec sp_tables --查看所有 + +exec sp_columns course + + +exec sp_password '123456','654321' + + +use studenttest +go +--没有输入,输出参数 procedure +--1. 定义存储过程查询学生姓名,年龄最小的学生信息 +create proc proc_GetMinAge +as +begin + select stuname, stuage from stuinfo where stuage = (select min(stuage) from stuinfo) +end + + +--drop procedure proc_GetMinAge + +exec proc_GetMinAge + + +--2.有输入参数,无输出参数, +-- 定义存储过程查询学生信息 +go +create proc proc_GetMinAgeByName +@stuName varchar(20) +as +begin + select * from stuinfo where stuname=@stuName +end +go + +exec proc_GetMinAgeByName '张三丰' + +--3.有输入参数,无输出参数, 有返回值:只返回 整型数据 函数:1.返回标量值(任意数据类型) 2.表值函数(返回一张表) +go +create proc proc_GetMinAgeById +@stuNo varchar(20) +as +begin + declare @age int + select @age=stuage from stuinfo where stuNo=@stuNo + set @stuNo = 'S2022' + return @age +end +go + +--drop procedure proc_GetMinAgeById + +--定义@age接收返回的值 +declare @age int +declare @stuid varchar(20) = 's2001' +exec @age = proc_GetMinAgeById @stuid +print @age +print @stuid + + + +--传入学生学号,返回姓名 +go +create proc proc_GetMinAgeBystuid +@stuid varchar(20) +as +begin + declare @stuName varchar(20) + select @stuName=stuname from stuinfo where stuNo=@stuid + --返回的类型是 非整型数据类型 此存储过程无效 + return @stuname +end +go + +--外部 +declare @name varchar(20) +exec @name = proc_GetMinAgeBystuid 'S2001' +print @name + + + +--4.有输入参数,有输出参数(引用数据类型) +--输入学号(stunum),定义存储过程实现输出学生信息 name school major +use STUDENTS +go + + +create proc proc_GetStuInfoById +@stuNum varchar(20), +@name varchar(20) output, --输出参数 +@school varchar(20) output, +@major varchar(20) output +as +begin + select @name=name, @school=school,@major=major from tb_student where stu_num=@stunum +end + +--定义四个变量 +declare @stunum varchar(20) = '16130203' +declare @name varchar(20),@school varchar(20),@major varchar(20) + +exec proc_GetStuInfoById @stunum,@name output, @school output , @major output +print @name + char(9) + @school + char(9) + @major + + + + + + + + + + + + + + + + +--5.一个变量同时具备输入,输出参数的存储过程 +go +create proc proc_GetMinAgeBystuNo +@stuNo varchar(20) output --output代表是输出参数 +as +begin + declare @age int + set @stuNo = 'S2022' +end +go + +declare @stuno varchar(20) = 'S2001' + +exec proc_GetMinAgeBystuNo @stuno output +print @stuno + + +--查询出某时间段的银行存取款信息以及存款总金额**,取款总金额, +--传入开始时间,结束时间,显示存取款交易信息的同时,返回存款总金额,取款总金额。 +use BankTest +go + +select * from cardExchange + + +create proc GetMoneyByTime +@startTime smalldatetime, +@endTime smalldatetime, +@TotalSaveMoney money output, +@TotalWithdrawMoney money output +as +begin + select @TotalSaveMoney = sum(MoneyInBank),@TotalWithdrawMoney=sum(MoneyOutBank) from cardExchange + where ExchangeTime between @startTime and @endTime +end + +-- +declare @startTime smalldatetime = '2022-09-07',@endTime smalldatetime = '2022-09-18' +declare @TotalSaveMoney money,@TotalWithdrawMoney money +exec GetMoneyByTime @startTime,@endTime,@TotalSaveMoney output , @TotalWithdrawMoney output +select @TotalSaveMoney 存款总额,@TotalWithdrawMoney 取款总额 \ No newline at end of file diff --git "a/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.30\346\270\270\346\240\207.md" "b/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.30\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..d00781fdbca2b089819018ff6b52f4ebb1cfe1bd --- /dev/null +++ "b/10\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.30\346\270\270\346\240\207.md" @@ -0,0 +1,65 @@ +游标 +什么是游标 +游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集。使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。 + +游标分类: + +(1)静态游标(Static):在操作游标的时候,数据发生变化,游标中数据不变 (2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中数据改变,默认值。 (3)键集驱动游标(KeySet):在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + +游标的使用 +创建游标: + +--1.创建游标(Scroll代表滚动游标,不加Scroll则是只进的,只能支持fetch next) +declare <游标名> cursor scroll for select stuname from stuinfo +打开游标: + +open <游标名> +关闭游标: + +close <游标名> +释放游标: + +deallocate <游标名> +提取数据操作: + +fetch first from <游标名> --结果集的第一行 +fetch last from <游标名> --最后一行 +fetch absolute 1 from <游标名> --从游标的第一行开始数,第n行。 +fetch relative 3 from <游标名> --从当前位置数,第n行。 +fetch next from <游标名> --当前位置的下一行 +fetch prior from <游标名> --当前位置的上一行 +例子: + +使用游标实现加薪操作 +use test_trigger +select * from People + + + --- 1.声明游标 + -- 1.1滚动游标 (scroll) +declare cur_UpdateSalaryById cursor scroll +for select PeopleId from People + + -- 2.打开游标 +open cur_UpdateSalaryById + -- 3 使用游标 + --声明变量来接受游标放回的值 与 存放加薪值 +declare @id int +declare @money money = 1000 + -- 注意事项: 使用变量存储游标值的格式 fetch ... from 游标名 into 变量名 +fetch first from cur_UpdateSalaryById into @id + --@@fetch_status = 0 : 还有下一行 -1: 提取失败 -2:提取空白行 + -- 每人都加薪1000 +while(@@fetch_status = 0) + begin + update People set PeopleSalary += @money where PeopleId = @id + print cast(@id as varchar(12)) + char(9) + cast(@money as varchar(20)) + fetch next from cur_UpdateSalaryById into @id + end + + -- 4.关闭游标 +close cur_UpdateSalaryById + + -- 5.释放变量 + +deallocate cur_UpdateSalaryById \ No newline at end of file