diff --git "a/05\345\215\240\345\277\227\346\226\260/9.30/.keep" "b/05\345\215\240\345\277\227\346\226\260/9.30/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/05\345\215\240\345\277\227\346\226\260/9.30/SQLQuery1.sql" "b/05\345\215\240\345\277\227\346\226\260/9.30/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..799e56e68d718fcfc1e1d6d7f4c51df6b597ea93 --- /dev/null +++ "b/05\345\215\240\345\277\227\346\226\260/9.30/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 + +--使用游标 实现:如果A表与B表中ID字段值相同,那么将B表中的省份,城市 修改成与A表中的城市一致(连表) +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/9.30/\346\270\270\346\240\207.txt" "b/05\345\215\240\345\277\227\346\226\260/9.30/\346\270\270\346\240\207.txt" new file mode 100644 index 0000000000000000000000000000000000000000..d68f23625f66d33e46c9181b712806ec6ec7c62c --- /dev/null +++ "b/05\345\215\240\345\277\227\346\226\260/9.30/\346\270\270\346\240\207.txt" @@ -0,0 +1,52 @@ +## 娓告爣 + +#### 浠涔堟槸娓告爣 + +娓告爣(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 <娓告爣鍚> --褰撳墠浣嶇疆鐨勪笂涓琛 \ No newline at end of file