From 7526e93606edddeac11106d995cab023f3f337ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E5=BE=B7=E6=A3=AE?= <1500225483@qq.com> Date: Thu, 6 Oct 2022 01:38:27 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-9-30-=E6=B8=B8?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-9-30-\346\270\270\346\240\207/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/.keep" diff --git "a/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/.keep" "b/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 553e6c17b1b4c3f5b66c2d709ae1695d4fd31a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E5=BE=B7=E6=A3=AE?= <1500225483@qq.com> Date: Thu, 6 Oct 2022 01:38:45 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=BE=E5=BE=B7=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 曾德森 <1500225483@qq.com> --- .../\344\275\234\344\270\232/SQLQuery2.sql" | 56 +++++++++++++++++++ .../\346\270\270\346\240\207.md" | 56 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 "47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\344\275\234\344\270\232/SQLQuery2.sql" create mode 100644 "47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\347\254\224\350\256\260/\346\270\270\346\240\207.md" diff --git "a/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\344\275\234\344\270\232/SQLQuery2.sql" "b/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\344\275\234\344\270\232/SQLQuery2.sql" new file mode 100644 index 0000000..af126a8 --- /dev/null +++ "b/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\344\275\234\344\270\232/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/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\347\254\224\350\256\260/\346\270\270\346\240\207.md" "b/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\347\254\224\350\256\260/\346\270\270\346\240\207.md" new file mode 100644 index 0000000..fbf2dd4 --- /dev/null +++ "b/47\346\233\276\345\276\267\346\243\256/2022-9-30-\346\270\270\346\240\207/\346\233\276\345\276\267\346\243\256/\347\254\224\350\256\260/\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 -- Gitee