diff --git "a/41\350\203\241\345\237\272\350\200\200/10.6/.keep" "b/41\350\203\241\345\237\272\350\200\200/10.6/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/41\350\203\241\345\237\272\350\200\200/10.6/10.6/2022-10-6\345\207\275\346\225\260.sql" "b/41\350\203\241\345\237\272\350\200\200/10.6/10.6/2022-10-6\345\207\275\346\225\260.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5d6cadd994091af17bf2d7941eeb764a3bc0d988 --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/10.6/10.6/2022-10-6\345\207\275\346\225\260.sql" @@ -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 diff --git "a/41\350\203\241\345\237\272\350\200\200/10.6/10.6/2022-10-6\347\254\224\350\256\260.txt" "b/41\350\203\241\345\237\272\350\200\200/10.6/10.6/2022-10-6\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..f46a16466b94b49896eb999df20fca549a40de80 --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/10.6/10.6/2022-10-6\347\254\224\350\256\260.txt" @@ -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 diff --git "a/41\350\203\241\345\237\272\350\200\200/9.30/.keep" "b/41\350\203\241\345\237\272\350\200\200/9.30/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/41\350\203\241\345\237\272\350\200\200/9.30/9.30/SQLQuery2.sql" "b/41\350\203\241\345\237\272\350\200\200/9.30/9.30/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..af126a8a984cebf4f60806b1bfb40e08c32d326f --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/9.30/9.30/SQLQuery2.sql" @@ -0,0 +1,56 @@ +3ʹαʵּн +--α +declare cur_id cursor scroll for select PeopleId from People +--α +open cur_id +--ȡ +declare @id int, @money money = 1000 +fetch first from cur_id into @id +while (@@fetch_status = 0) + begin + update People set PeopleSalary += @money where PeopleId = @id + fetch next from cur_id into @id + end +--رα +close cur_id +select * from People +go + +--4:ʹαɾԱ +select * from People +-- +declare cur_id cursor scroll for select PeopleId,PeopleSalary from People +-- +open cur_id +--ȡ +declare @id int, @money money +fetch first from cur_id into @id,@money +while(@@FETCH_STATUS = 0) + begin + if(@money < 10000) + begin + delete People where PeopleId = @id + fetch next from cur_id into @id,@money + end + else + fetch next from cur_id into @id,@money + end +close cur_id +select * from People +go +--ʹα ʵ:ABIDֵֶͬôBеʡݣ ޸ijAеijһ£ + +declare cur_id cursor scroll for select A.* from A inner join B on A.id = B.id +deallocate cur_id +open cur_id +declare @id int , @id1 int,@Province varchar(10),@City varchar(10) +fetch first from cur_id into @id,@Province,@City +while(@@FETCH_STATUS = 0) + begin + update B set B.Province = @Province , B.City = @City where B.id = @id + fetch next from cur_id into @id,@Province,@City + end + +close cur_id +select * from A +select * from B \ No newline at end of file diff --git "a/41\350\203\241\345\237\272\350\200\200/9.30/9.30/\346\270\270\346\240\207.md" "b/41\350\203\241\345\237\272\350\200\200/9.30/9.30/\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..fbf2dd42d5220b0b25f759e096f5709bcd8bf274 --- /dev/null +++ "b/41\350\203\241\345\237\272\350\200\200/9.30/9.30/\346\270\270\346\240\207.md" @@ -0,0 +1,56 @@ +## 1.1游标的基本概念 + +游标是一种处理数据的方法,具有对结果集进行逐行处理的能力 + +## 1-2 游标的实现功能及使用步骤 + +**游标的实现功能** + +允许对 SELECT 返回的表中的每一行进行相同或不同的操作,而不是一次对整个结果集进行同一种操作; 从表中的当前位置检索一行或多行数据; 游标允许应用程序对当前位置的数据进行修改、删除的能力; 对于不同用户对结果集包含的数据所做的修改,支持不同的可见性级别; 游标的使用步骤 + +- 创建游标 + +``` +declare 游标名 cursor scroll for select查询语句 + +游标分为局部游标和全局游标两种,local表示局部游标,global表示全局游标(默认值,可以省略)。当指定 forward_only(默认值,可以省略)时,游标是只进的,也就是说只能从头到尾地提取记录,如果需要在行之间来回跳跃,需要指定为scroll。 +``` + +- 打开游标 +- 读取内容 +- 关闭游标 +- 释放游标(删除) + +``` +deallocate (游标名) +``` + +**提取数据操作:** + +``` +fetch first from <游标名> --结果集的第一行 +fetch last from <游标名> --最后一行 +fetch absolute 1 from <游标名> --从游标的第一行开始数,第n行。 +fetch relative 3 from <游标名> --从当前位置数,第n行。 +fetch next from <游标名> --当前位置的下一行 +fetch prior from <游标名> --当前位置的上一行 +``` + +**游标分为静态游标和动态游标,静态游标的数据是固定的,不会因为数据表的改变而改变;动态游标的数据是随着数据表变化而变化的,游标默认是动态游标** + +``` +DECLARE @id INT , @name NVARCHAR(50) --声明变量,需要读取的数据 +DECLARE cur CURSOR --去掉STATIC关键字即可 +FOR + SELECT * FROM #T +OPEN cur --打开游标 +FETCH NEXT FROM cur INTO @id, @name --取数据 +WHILE ( @@fetch_status = 0 ) --判断是否还有数据 + BEGIN + SELECT '数据: ' + RTRIM(@id) + @name + UPDATE #T SET name='测试' WHERE id=4 --测试静态动态用 + FETCH NEXT FROM cur INTO @id, @name --这里一定要写取下一条数据 + END +CLOSE cur --关闭游标 +DEALLOCATE cur +``` \ No newline at end of file