diff --git "a/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/.keep" "b/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/\346\270\270\346\240\207\347\254\224\350\256\260.txt" "b/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/\346\270\270\346\240\207\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..c93158ac5ebc886d75e9ac67655f68d59c56fa39 --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/\346\270\270\346\240\207\347\254\224\350\256\260.txt" @@ -0,0 +1,53 @@ +## 游标 + +#### 什么是游标 + +游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集。使用游标(cursor)的一个主要的原因就是把集合操作转换成**单个记录处理方式**。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。 + +游标分类: + +(1)静态游标(Static):在操作游标的时候,数据发生变化,游标中数据不变 +(2)动态游标(Dynamic):在操作游标的时候,数据发生变化,游标中数据改变,默认值。 +(3)键集驱动游标(KeySet):在操作游标的时候,被标识的列发生改变,游标中数据改变,其他列改变,游标中数据不变。 + + + +#### 游标的使用 + +**创建游标:** + +```sql +--1.创建游标(Scroll代表滚动游标,不加Scroll则是只进的,只能支持fetch next) +declare <游标名> cursor scroll for select stuname from stuinfo +``` + +**打开游标:** + +```sql +open <游标名> +``` + +**关闭游标:** + +``` +close <游标名> +``` + +**释放游标:** + +``` +deallocate <游标名> +``` + + + +**提取数据操作:** + +```sql +fetch first from <游标名> --结果集的第一行 +fetch last from <游标名> --最后一行 +fetch absolute 1 from <游标名> --从游标的第一行开始数,第n行。 +fetch relative 3 from <游标名> --从当前位置数,第n行。 +fetch next from <游标名> --当前位置的下一行 +fetch prior from <游标名> --当前位置的上一行 +``` diff --git "a/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/\350\257\276\345\240\202\346\241\210\344\276\213.sql" "b/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/\350\257\276\345\240\202\346\241\210\344\276\213.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2a2aa8811fc111beaea2e3812beb6e1e10a9eefa --- /dev/null +++ "b/09\346\235\216\346\266\233/2022-09-30\357\274\210\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\357\274\211/\350\257\276\345\240\202\346\241\210\344\276\213.sql" @@ -0,0 +1,180 @@ +--ԣԭԣһԣԣ־ +--4뼶 +-- δύ ظö +-- ύ ظö +-- ظ: ֻö +-- л +dbcc useroptions + + + +--α:ڴ洢 +use test_trigger +go + + + + + + +select * from People +--ãԷصĽеÿһне +-- +--create index +--create view + +--α +--ֻα꣺αеֻ²ѯ֮ܲǰ +--α (scroll)ʱָһ +declare cur_UpdateSalaryById cursor scroll +for select PeopleId from People + +--ȴ +open cur_UpdateSalaryById + +--ȡ: +--next:һ +--priorһ +--first:һ +--lastһ +--absolute nԵĵn +--relative n: Ե + + +--ѭ㣺first յ㣺һ(ж) ѭ壺update + +--@@fetch_status = 0 : һ -1: ȡʧ -2:ȡհ + + + +declare @id int +declare @mon money =1000 +fetch first from cur_UpdateSalaryById into @id +while(@@FETCH_STATUS = 0) + begin + --Ǯ + update people set PeopleSalary -=@mon where PeopleId=@id + --ӡ + print cast(@id as varchar(2) ) + char(9) + cast (@mon as varchar(10)) + set @mon += 1000 + --ȡһ + fetch next from cur_UpdateSalaryById into @id + end + + +--ر +close cur_UpdateSalaryById + + +--ͷ +deallocate cur_UpdateSalaryById + + +--1ʹαűΪ009Ա͹ +--1. +declare cur_OutputPeopleById cursor scroll +for select PeopleID,PeopleName,PeopleSalary from People +--2. +open cur_OutputPeopleById +--3.ʹ +declare @peopleid varchar(5), @name varchar(20), @salary varchar(10) +fetch first from cur_OutputPeopleById into @peopleid,@name,@salary +while(@@FETCH_STATUS = 0) + if(@peopleid = 6) + begin + --ӡ + print @peopleid + char(9) + @name + char(9) + @salary + break + end + else + fetch next from cur_OutputPeopleById into @peopleid,@name,@salary + + +select * from People + + + + + + + + + + +deallocate cur_OutputPeopleById + + +--ʹαŵIJźţ߹ʣ͹ +select * from People +--deallocate cur_GetSalaryById +declare cur_GetSalaryById cursor scroll +for select Departmentid,PeopleSalary from People + +open cur_GetSalaryById + +declare @departmentid varchar(10) +fetch first from cur_GetSalaryById into @departmentid,@salary +while(@@FETCH_STATUS = 0) + begin + declare @maxSalary varchar(20), @minSalary varchar(20) + --нˮ + --select @maxSalary= peoplesalary ,@minsalary = PeopleSalary from People + + end + +--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