From d753ca9931c52f394090214d364e864228205454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=85=88=E6=9D=B0?= <19892226650@139.com> Date: Thu, 6 Oct 2022 02:24:31 +0000 Subject: [PATCH 1/2] =?UTF-8?q?38=E5=BC=A0=E5=85=88=E6=9D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张先杰 <19892226650@139.com> --- ...344\270\232--\346\270\270\346\240\207.sql" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "38\345\274\240\345\205\210\346\235\260/\344\275\234\344\270\232/2022.9.30\344\275\234\344\270\232--\346\270\270\346\240\207.sql" diff --git "a/38\345\274\240\345\205\210\346\235\260/\344\275\234\344\270\232/2022.9.30\344\275\234\344\270\232--\346\270\270\346\240\207.sql" "b/38\345\274\240\345\205\210\346\235\260/\344\275\234\344\270\232/2022.9.30\344\275\234\344\270\232--\346\270\270\346\240\207.sql" new file mode 100644 index 0000000..daa8019 --- /dev/null +++ "b/38\345\274\240\345\205\210\346\235\260/\344\275\234\344\270\232/2022.9.30\344\275\234\344\270\232--\346\270\270\346\240\207.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 -- Gitee From e4bea774bdd1e63fe286eb517914938ed8b60bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=85=88=E6=9D=B0?= <19892226650@139.com> Date: Thu, 6 Oct 2022 02:24:53 +0000 Subject: [PATCH 2/2] =?UTF-8?q?38=E5=BC=A0=E5=85=88=E6=9D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张先杰 <19892226650@139.com> --- ...\350\256\260--\346\270\270\346\240\207.md" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "38\345\274\240\345\205\210\346\235\260/\347\254\224\350\256\260/2022.9.30\347\254\224\350\256\260--\346\270\270\346\240\207.md" diff --git "a/38\345\274\240\345\205\210\346\235\260/\347\254\224\350\256\260/2022.9.30\347\254\224\350\256\260--\346\270\270\346\240\207.md" "b/38\345\274\240\345\205\210\346\235\260/\347\254\224\350\256\260/2022.9.30\347\254\224\350\256\260--\346\270\270\346\240\207.md" new file mode 100644 index 0000000..ab855cf --- /dev/null +++ "b/38\345\274\240\345\205\210\346\235\260/\347\254\224\350\256\260/2022.9.30\347\254\224\350\256\260--\346\270\270\346\240\207.md" @@ -0,0 +1,67 @@ + + +## 1.1游标的基本概念 + +游标是一种处理数据的方法,具有对结果集进行逐行处理的能力 + +## 1-2 游标的实现功能及使用步骤 + +**游标的实现功能** + +允许对 SELECT 返回的表中的每一行进行相同或不同的操作,而不是一次对整个结果集进行同一种操作; +从表中的当前位置检索一行或多行数据; +游标允许应用程序对当前位置的数据进行修改、删除的能力; +对于不同用户对结果集包含的数据所做的修改,支持不同的可见性级别; +游标的使用步骤 + +- 创建游标 + +```sql +declare 游标名 cursor scroll for select查询语句 + +游标分为局部游标和全局游标两种,local表示局部游标,global表示全局游标(默认值,可以省略)。当指定 forward_only(默认值,可以省略)时,游标是只进的,也就是说只能从头到尾地提取记录,如果需要在行之间来回跳跃,需要指定为scroll。 + +``` + +- 打开游标 + +- 读取内容 +- 关闭游标 +- 释放游标(删除) + +```sql +deallocate (游标名) +``` + +**提取数据操作:** + +```sql +fetch first from <游标名> --结果集的第一行 +fetch last from <游标名> --最后一行 +fetch absolute 1 from <游标名> --从游标的第一行开始数,第n行。 +fetch relative 3 from <游标名> --从当前位置数,第n行。 +fetch next from <游标名> --当前位置的下一行 +fetch prior from <游标名> --当前位置的上一行 +``` + + **游标分为静态游标和动态游标,静态游标的数据是固定的,不会因为数据表的改变而改变;动态游标的数据是随着数据表变化而变化的,游标默认是动态游标** + +```sql + +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 + +``` + -- Gitee