From bc0b23fb18384b843bfe14b2b43f6fdca01d7dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Wed, 5 Oct 2022 08:43:17 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E4=B9=9D?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 5d4449edb178096c2a5ef840658334c90c705049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Wed, 5 Oct 2022 08:43:35 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 060a0b6f7de39094aad41de1026ff600cccf2327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Wed, 5 Oct 2022 08:43:45 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 3848474da6be16fe2a2886077344d58cb09b74e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Wed, 5 Oct 2022 08:44:08 +0000 Subject: [PATCH 4/6] =?UTF-8?q?add=2022=20=E6=9D=A8=E5=87=8C=E7=BF=94/?= =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C=E4=B8=9A/=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../\344\275\234\344\270\232/test.sql" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" new file mode 100644 index 0000000..ec33d65 --- /dev/null +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\344\275\234\344\270\232/test.sql" @@ -0,0 +1,49 @@ +--使用游标实现加薪操作 +declare huhu cursor scroll +for select * from People +open huhu +declare @PeopleId int +fetch first from huhu into @PeopleId +while (@@FETCH_STATUS=0) + begin + update People set PeopleSalary+=1000 where PeopleId=@PeopleId + fetch next from huhu into @PeopleId + end +close huhu +deallocate huhu +select * from People +--使用游标删除不达标员工 +declare huhu cursor scroll +for select * from People +open huhu +declare @id int +fetch first from huhu into @id +while (@@FETCH_STATUS=0) + begin + declare @money int=(select PeopleSalary from People where PeopleId=@id) + + if(@money<5000) + begin + delete People where PeopleId = @id + end + fetch next from huhu into @id + end +close huhu +deallocate huhu +select * from People +--使用游标 实现:如果A表与B表中ID字段值相同,那么将B表中的省份,城市 修改成与A表中的城市一致(连表) +declare cur_id cursor scroll +for select A.* from A inner join B on A.id = B.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 +deallocate cur_id +select * from A +select * from Bt \ No newline at end of file -- Gitee From ac34356c70dbdcbe77a6df60f7f1ee318de1d77a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Wed, 5 Oct 2022 08:46:17 +0000 Subject: [PATCH 5/6] =?UTF-8?q?add=2022=20=E6=9D=A8=E5=87=8C=E7=BF=94/?= =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C=E4=B8=9A/=E7=AC=94?= =?UTF-8?q?=E8=AE=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../2022-10-5-\346\270\270\346\240\207.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-5-\346\270\270\346\240\207.md" diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-5-\346\270\270\346\240\207.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-5-\346\270\270\346\240\207.md" new file mode 100644 index 0000000..e69de29 -- Gitee From aeb0b1fb3f3b420b7f2136b24e20ae2acc7586cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=87=8C=E7=BF=94?= <963304364@qq.com> Date: Wed, 5 Oct 2022 08:46:33 +0000 Subject: [PATCH 6/6] =?UTF-8?q?update=2022=20=E6=9D=A8=E5=87=8C=E7=BF=94/?= =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C=E4=B8=9A/=E7=AC=94?= =?UTF-8?q?=E8=AE=B0/2022-10-5-=E6=B8=B8=E6=A0=87.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨凌翔 <963304364@qq.com> --- .../2022-10-5-\346\270\270\346\240\207.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-5-\346\270\270\346\240\207.md" "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-5-\346\270\270\346\240\207.md" index e69de29..24c1669 100644 --- "a/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-5-\346\270\270\346\240\207.md" +++ "b/22 \346\235\250\345\207\214\347\277\224/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\224\350\256\260/2022-10-5-\346\270\270\346\240\207.md" @@ -0,0 +1,52 @@ +## 游标 + +#### 什么是游标 + +游标(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 <游标名> --当前位置的上一行 -- Gitee