From 4fae08060f96c378624054e52892a5d3c9edf38c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98=E5=90=B4=E6=96=87=E9=BE=99=E2=80=99?= <2717752887@qq.com> Date: Thu, 6 Oct 2022 11:49:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E5=92=8C=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-10-5\346\270\270\346\240\207.sql" | 70 +++++++++++ ...35\346\254\241\347\254\224\350\256\260.md" | 119 ++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 "17\345\220\264\346\226\207\351\276\231/\344\275\234\344\270\232/2022-10-5\346\270\270\346\240\207.sql" create mode 100644 "17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-10-5\347\254\254\344\271\235\346\254\241\347\254\224\350\256\260.md" diff --git "a/17\345\220\264\346\226\207\351\276\231/\344\275\234\344\270\232/2022-10-5\346\270\270\346\240\207.sql" "b/17\345\220\264\346\226\207\351\276\231/\344\275\234\344\270\232/2022-10-5\346\270\270\346\240\207.sql" new file mode 100644 index 0000000..c284d98 --- /dev/null +++ "b/17\345\220\264\346\226\207\351\276\231/\344\275\234\344\270\232/2022-10-5\346\270\270\346\240\207.sql" @@ -0,0 +1,70 @@ +select * from Department +select * from People +--使用游标实现加薪操作 +declare cur_id cursor scroll for select PeopleId from People +open cur_id +declare @a int, @b int=1000 +fetch first from cur_id into @a +while(@@FETCH_STATUS=0) +begin + update People set PeopleSalary+=@b where PeopleId=@a + fetch next from cur_id into @a +end +select * from People + +close cur_id +deallocate cur_id +--使用游标删除不达标员工 +declare cur_id cursor scroll for select PeopleId , PeopleSalary from People +open cur_id +declare @c int,@d money +fetch first from cur_id into @c,@d +while(@@FETCH_STATUS=0) +begin + if(@d<10000) + begin + delete people where PeopleId=@c + fetch next from cur_id into @c,@d + end + else + fetch next from cur_id into @c,@d + end + +close cur_id +deallocate cur_id + +--使用游标 实现:如果A表与B表中ID字段值相同,那么将B表中的省份,城市 修改成与A表中的城市一致(连表) +create table a1( +id int , +province varchar(20), +city varchar(20) +) +create table a2( +id int , +province varchar(20), +city varchar(20) +) +insert into a1(id,province,city)values(1,'福建','厦门') +insert into a1(id,province,city)values(2,'广东','广州') +insert into a1(id,province,city)values(3,'福建','龙岩') +insert into a1(id,province,city)values(4,'云南','昆明') +insert into a2(id,province,city)values(1,'福建','厦门') +insert into a2(id,province,city)values(3,'江西','南昌') +insert into a2(id,province,city)values(5,'湖北','武汉') +insert into a2(id,province,city)values(7,'湖南','长沙') + +select * from a1 +select * from a2 + +declare cur_id cursor scroll for select a1.* from a1 join a2 on a1.id=a2.id +open cur_id +declare @id int,@province varchar(20),@city varchar(20) +fetch first from cur_id into @id,@province,@city +while(@@FETCH_STATUS=0) +begin + update a2 set a2.province=@province,a2.city=@city where a2.id=@id + fetch next from cur_id into @id,@province,@city +end +close cur_id +deallocate cur_id + diff --git "a/17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-10-5\347\254\254\344\271\235\346\254\241\347\254\224\350\256\260.md" "b/17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-10-5\347\254\254\344\271\235\346\254\241\347\254\224\350\256\260.md" new file mode 100644 index 0000000..94cd75e --- /dev/null +++ "b/17\345\220\264\346\226\207\351\276\231/\347\254\224\350\256\260/2022-10-5\347\254\254\344\271\235\346\254\241\347\254\224\350\256\260.md" @@ -0,0 +1,119 @@ +## 娓告爣 + +#### 浠涔堟槸娓告爣 + +娓告爣(Cursor)瀹冧娇鐢ㄦ埛鍙愯璁块棶鐢盨QL Server杩斿洖鐨勭粨鏋滈泦銆備娇鐢ㄦ父鏍(cursor)鐨勪竴涓富瑕佺殑鍘熷洜灏辨槸鎶婇泦鍚堟搷浣滆浆鎹㈡垚**鍗曚釜璁板綍澶勭悊鏂瑰紡**銆傜敤SQL璇█浠庢暟鎹簱涓绱㈡暟鎹悗锛岀粨鏋滄斁鍦ㄥ唴瀛樼殑涓鍧楀尯鍩熶腑锛屼笖缁撴灉寰寰鏄竴涓惈鏈夊涓褰曠殑闆嗗悎銆傛父鏍囨満鍒跺厑璁哥敤鎴峰湪SQL server鍐呴愯鍦拌闂繖浜涜褰曪紝鎸夌収鐢ㄦ埛鑷繁鐨勬剰鎰挎潵鏄剧ず鍜屽鐞嗚繖浜涜褰曘 + +娓告爣鍒嗙被: + +锛1锛夐潤鎬佹父鏍(Static)锛氬湪鎿嶄綔娓告爣鐨勬椂鍊欙紝鏁版嵁鍙戠敓鍙樺寲锛屾父鏍囦腑鏁版嵁涓嶅彉 +锛2锛夊姩鎬佹父鏍(Dynamic)锛氬湪鎿嶄綔娓告爣鐨勬椂鍊欙紝鏁版嵁鍙戠敓鍙樺寲锛屾父鏍囦腑鏁版嵁鏀瑰彉锛岄粯璁ゅ笺 +锛3锛夐敭闆嗛┍鍔ㄦ父鏍(KeySet)锛氬湪鎿嶄綔娓告爣鐨勬椂鍊欙紝琚爣璇嗙殑鍒楀彂鐢熸敼鍙橈紝娓告爣涓暟鎹敼鍙橈紝鍏朵粬鍒楁敼鍙橈紝娓告爣涓暟鎹笉鍙樸 + + + +#### 娓告爣鐨勪娇鐢 + +**鍒涘缓娓告爣锛** + +```sql +--1.鍒涘缓娓告爣(Scroll浠h〃婊氬姩娓告爣锛屼笉鍔燬croll鍒欐槸鍙繘鐨勶紝鍙兘鏀寔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 <娓告爣鍚> --褰撳墠浣嶇疆鐨勪笂涓琛 +``` + +**鎻愬彇鏁版嵁缁欏彉閲忎互渚涘畠鐢紙鍙栧嚭绗3琛屽鐢熷鍚嶏紝鏌ヨ璇ュ鐢熻缁嗕俊鎭級锛** + +```sql +declare @vari varchar(20) +fetch absolute 3 from <娓告爣鍚> into @vari +select * from StuInfo where stuName = @vari +``` + +**鍒╃敤娓告爣鎻愬彇鎵鏈夌殑瀛︾敓淇℃伅锛** + +```sql +--鏂规涓锛 +fetch absolute 1 from <娓告爣鍚> +while @@FETCH_STATUS = 0 --@@FETCH_STATUS=0,鎻愬彇鎴愬姛锛-1鎻愬彇澶辫触锛-2琛屼笉瀛樺湪 + begin + fetch next from <娓告爣鍚> + end + +--鏂规浜岋細 +declare @myvar varchar(20) +fetch first from <娓告爣鍚> into @myvar +while @@FETCH_STATUS = 0 + begin + print '鎻愬彇鎴愬姛'+@myvar + fetch next from <娓告爣鍚> into @myvar + end +``` + +**鍒╃敤娓告爣淇敼鍜屽垹闄ゆ暟鎹細** + +```sql +--鏇存柊璇硶: +-- fetch absolute 3 from <娓告爣鍚> +-- update 璇彞 where Current of <娓告爣鍚> + + +--缁冧範1锛氫慨鏀瑰懆椋樼殑鐝骇id涓1 + +--缁冧範2:浣跨敤鍒犻櫎濮撳悕涓烘浘楣忕殑涓汉淇℃伅 + +``` + +**鍒涘缓娓告爣鎸囧悜鏌愯澶氬垪鏁版嵁,骞跺惊鐜樉绀烘暟鎹細** + +```sql +--姝ゅ濡傛灉鎸囧悜鎵鏈夋暟鎹紝鍙互灏唂or鍚庨潰鐨勮鍙ヤ慨鏀规垚select * from stuinfo +declare <娓告爣鍚> cursor scroll +declare mycursor cursor scroll for select stuname,StuSex,ClassID from stuinfo + +open <娓告爣鍚> + +declare @name varchar(20) +declare @sex varchar(2) +declare @cid int +fetch first from mycursor into @name,@sex,@cid +while @@FETCH_STATUS=0 +fetch next from <娓告爣鍚> into into @name,@sex,@cid + +while @@FETCH_STATUS = 0 --@@FETCH_STATUS=0,鎻愬彇鎴愬姛锛-1鎻愬彇澶辫触锛-2琛屼笉瀛樺湪 + begin + print '鎻愬彇鎴愬姛' + fetch next from CURSORMember into@into @name,@sex,@cid + end +close <娓告爣鍚> +``` \ No newline at end of file -- Gitee