diff --git "a/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/9.30\344\275\234\344\270\232.txt" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/9.30\344\275\234\344\270\232.txt" new file mode 100644 index 0000000000000000000000000000000000000000..ee80ec88b25ee61e94241c8ef19060920d5291e0 --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\344\275\234\344\270\232/9.30\344\275\234\344\270\232.txt" @@ -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 +--使用游标 实现:如果A表与B表中ID字段值相同,那么将B表中的省份,城市 修改成与A表中的城市一致(连表) + +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/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/9.30\347\254\224\350\256\260.txt" "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/9.30\347\254\224\350\256\260.txt" new file mode 100644 index 0000000000000000000000000000000000000000..086312c70a0f17edcca0f0498763275517c200bf --- /dev/null +++ "b/13\345\217\267\351\203\221\346\226\207\346\272\220/\347\254\224\350\256\260/9.30\347\254\224\350\256\260.txt" @@ -0,0 +1,46 @@ +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