diff --git "a/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\270\270\346\240\207.md" "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..015a3e01468b3e018ad2a6542160f1874bf8d53b --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\344\275\234\344\270\232/\346\270\270\346\240\207.md" @@ -0,0 +1,100 @@ +``` +use test_trigger +go + +select * from People +select * from Department + +--例3:使用游标实现加薪操作 +declare cur_id cursor scroll +for select PeopleId from People + +open cur_id + +declare @p_Id int,@p_salary money = 2000 +fetch first from cur_id into @p_Id + +while(@@FETCH_STATUS = 0) +begin + update People set PeopleSalary += @p_salary where PeopleId = @p_Id + fetch next from cur_id into @p_Id +end + +select * from people + +close cur_id + +--例4:使用游标删除不达标员工 +declare cur_del cursor scroll +for select PeopleId,PeopleSalary from People + +open cur_del + +declare @pId int,@pSalary money +fetch first from cur_del into @pId,@pSalary + +while(@@FETCH_STATUS = 0) +begin + if(@pSalary < 10000) + begin + delete People where PeopleId = @pId + fetch next from cur_del into @pId,@pSalary + end + + fetch next from cur_del into @pId,@pSalary +end + +select * from people + +close cur_del + +--使用游标 实现:如果A表与B表中ID字段值相同,那么将B表中的省份,城市 修改成与A表中的城市一致(连表) +create database AB +go +use AB +go +create table A( +aId int not null, +aProvince varchar(50), +aCity varchar(50) +) +go +create table B( +bId int not null, +bProvince varchar(50), +bCity varchar(50) +) +go +insert into A(aId,aProvince,aCity) values +(1,'安徽','淮南'), +(6,'广东','深圳'), +(7,'福建','龙岩'), +(8,'甘肃','兰州') +go +insert into B(bId,bProvince,bCity) values +(1,'河北','石家庄'), +(2,'广东','汕头'), +(3,'福建','厦门'), +(4,'山西','太原') +go + +--deallocate cur_city +declare cur_city cursor scroll +for select A.* from A join B on A.aId = B.bId + +open cur_city + +declare @aid int,@apro varchar(50),@acity varchar(50) +fetch first from cur_city into @aid,@apro,@acity + +while(@@FETCH_STATUS = 0) +begin + update B set B.bProvince = @apro,B.bCity = @acity where B.bId = @aid + fetch next from cur_city into @aid,@apro,@acity +end + +select * from A +select * from B + +close cur_city +``` \ 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-06\346\270\270\346\240\207.md" "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-06\346\270\270\346\240\207.md" new file mode 100644 index 0000000000000000000000000000000000000000..a455c1fb424c114c318e6e96087184a0a9374c54 --- /dev/null +++ "b/31\345\220\264\346\254\243\347\207\225/\347\254\224\350\256\260/2022-10-06\346\270\270\346\240\207.md" @@ -0,0 +1,67 @@ +### 游标 Cursor + +游标可以使用户逐行访问由SQL Server返回的结果集,能把集合操作转换成**单个记录处理方式**,按照用户自己的意愿来显示和处理这些记录。 + +#### 游标分类: + +(1)静态游标(Static):在操作游标的时候,数据发生变化,游标中数据不变 (2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中数据改变,默认值。 (3)键集驱动游标(KeySet):在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + +#### 游标的使用 + +``` +--创建游标(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 <游标名> --当前位置的上一行 +``` + +**提取数据给变量以供它用(取出第3行学生姓名,查询该学生详细信息):** + +``` +declare @vari varchar(20) +fetch absolute 3 from <游标名> into @vari +select * from StuInfo where stuName = @vari +``` + +**利用游标提取所有的学生信息:** + +``` +--方案一: +fetch absolute 1 from <游标名> +while @@FETCH_STATUS = 0 --@@FETCH_STATUS=0,提取成功,-1提取失败,-2行不存在 + begin + fetch next from <游标名> + end + +--方案二: +declare @myvar varchar(20) +fetch first from <游标名> into @myvar +while @@FETCH_STATUS = 0 + begin + print '提取成功'+@myvar + fetch next from <游标名> into @myvar + end +``` + +**利用游标修改和删除数据:** + +``` +--更新语法: +-- fetch absolute 3 from <游标名> +-- update 语句 where Current of <游标名> +``` \ No newline at end of file