From 2fe3c0a92f84d696aaa6e35946d1ddab8497c263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A0=E5=BF=97=E6=96=B0?= <1759998065@qq.com> Date: Fri, 7 Oct 2022 05:25:42 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=2010.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "05\345\215\240\345\277\227\346\226\260/10.6/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "05\345\215\240\345\277\227\346\226\260/10.6/.keep" diff --git "a/05\345\215\240\345\277\227\346\226\260/10.6/.keep" "b/05\345\215\240\345\277\227\346\226\260/10.6/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 3bc8c5ced4230c8b618cd93212589dcd53f89d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A0=E5=BF=97=E6=96=B0?= <1759998065@qq.com> Date: Fri, 7 Oct 2022 05:26:06 +0000 Subject: [PATCH 2/2] =?UTF-8?q?05=E5=8D=A0=E5=BF=97=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 占志新 <1759998065@qq.com> --- .../10.6/SQLQuery1.sql" | 98 +++++++++++++++++++ .../10.6/\346\270\270\346\240\207.txt" | 52 ++++++++++ 2 files changed, 150 insertions(+) create mode 100644 "05\345\215\240\345\277\227\346\226\260/10.6/SQLQuery1.sql" create mode 100644 "05\345\215\240\345\277\227\346\226\260/10.6/\346\270\270\346\240\207.txt" diff --git "a/05\345\215\240\345\277\227\346\226\260/10.6/SQLQuery1.sql" "b/05\345\215\240\345\277\227\346\226\260/10.6/SQLQuery1.sql" new file mode 100644 index 0000000..799e56e --- /dev/null +++ "b/05\345\215\240\345\277\227\346\226\260/10.6/SQLQuery1.sql" @@ -0,0 +1,98 @@ +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 + +--ʹα ʵ:ABIDֵֶͬôBеʡݣ ޸ijAеijһ£ +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/05\345\215\240\345\277\227\346\226\260/10.6/\346\270\270\346\240\207.txt" "b/05\345\215\240\345\277\227\346\226\260/10.6/\346\270\270\346\240\207.txt" new file mode 100644 index 0000000..d68f236 --- /dev/null +++ "b/05\345\215\240\345\277\227\346\226\260/10.6/\346\270\270\346\240\207.txt" @@ -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 <游标名> --当前位置的上一行 \ No newline at end of file -- Gitee