1 select studentid,studentname,score from students;
1 DECLARE cursor_name CURSOR FOR select_statement;
1 OPEN cursor_name;
1 FETCH cursor_name INTO var_list;
1 CLOSE cursor_name;
1 mysql> select * from students; 2 +-----------+-------------+-------+---------+ 3 | studentid | studentname | score | classid | 4 +-----------+-------------+-------+---------+ 5 | 1 | brand | 97.5 | 1 | 6 | 2 | helen | 96.5 | 1 | 7 | 3 | lyn | 96 | 1 | 8 | 4 | sol | 97 | 1 | 9 | 5 | b1 | 81 | 2 | 10 | 6 | b2 | 82 | 2 | 11 | 7 | c1 | 71 | 3 | 12 | 8 | c2 | 72.5 | 3 | 13 | 9 | lala | 73 | 0 | 14 | 10 | A | 99 | 3 | 15 | 16 | test1 | 100 | 0 | 16 | 17 | trigger2 | 107 | 0 | 17 | 22 | trigger1 | 100 | 0 | 18 +-----------+-------------+-------+---------+ 19 13 rows in set
這邊註釋很清晰,關鍵知識點都已經標紅mysql
1 mysql> 2 /*判斷函數若是存在則刪除*/ 3 DROP FUNCTION IF EXISTS fun_test; 4 /*聲明結束符爲$*/ 5 DELIMITER $ 6 /*建立函數,對符合條件的每一個同窗的分數進行加分,加的分數不能超過給定的值max_score*/ 7 CREATE FUNCTION fun_test(max_score decimal(10,2)) 8 RETURNS int 9 BEGIN 10 /*定義實時StudentId的變量*/ 11 DECLARE var_studentId int DEFAULT 0; 12 /*定義計算後分數的變量*/ 13 DECLARE var_score decimal(10,2) DEFAULT 0; 14 /*定義遊標結束標誌變量*/ 15 DECLARE var_done int DEFAULT FALSE; 16 /*建立遊標*/ 17 DECLARE cur_test CURSOR FOR SELECT studentid,score from students where classid<>0; 18 /*遊標結束時會設置var_done爲true,後續能夠使用var_done來判斷遊標是否結束*/ 19 DECLARE CONTINUE HANDLER FOR NOT FOUND SET var_done=TRUE; 20 /*打開遊標*/ 21 OPEN cur_test; 22 /*使用Loop循環遍歷遊標*/ 23 select_loop:LOOP 24 /*先獲取當前行的數據,而後將當前行的數據放入var_studentId,var_score中,若是無數據行了,var_done會被置爲true*/ 25 FETCH cur_test INTO var_studentId,var_score; 26 /*經過var_done來判斷遊標是否結束了,退出循環*/ 27 IF var_done THEN 28 LEAVE select_loop; 29 END IF; 30 /*對var_score值添加隨機值,不能超過給定的分數*/ 31 set var_score = var_score + LEAST(ROUND(rand()*10,0),max_score); 32 update students set score = var_score where studentId= var_studentId; 33 END LOOP; 34 /*關閉遊標*/ 35 CLOSE cur_test; 36 /*返回結果:能夠根據實際狀況返回須要的內容*/ 37 RETURN 1; 38 END $ 39 /*結束符置爲;*/ 40 DELIMITER ; 41 Query OK, 0 rows affected
1 mysql> 2 /* 參數爲8,表示加分上限爲8 */ 3 select fun_test(8); 4 +-------------+ 5 | fun_test(8) | 6 +-------------+ 7 | 1 | 8 +-------------+ 9 1 row in set
對比原來的成績的值,發現成績添加了隨機值,但沒超過給定的分數 8sql
1 mysql> select * from students; 2 +-----------+-------------+-------+---------+ 3 | studentid | studentname | score | classid | 4 +-----------+-------------+-------+---------+ 5 | 1 | brand | 105.5 | 1 | 6 | 2 | helen | 98.5 | 1 | 7 | 3 | lyn | 97 | 1 | 8 | 4 | sol | 97 | 1 | 9 | 5 | b1 | 89 | 2 | 10 | 6 | b2 | 90 | 2 | 11 | 7 | c1 | 76 | 3 | 12 | 8 | c2 | 73.5 | 3 | 13 | 9 | lala | 73 | 0 | 14 | 10 | A | 100 | 3 | 15 | 16 | test1 | 100 | 0 | 16 | 17 | trigger2 | 107 | 0 | 17 | 22 | trigger1 | 100 | 0 | 18 +-----------+-------------+-------+---------+ 19 13 rows in set
符合條件被修改分數的有9條數據,都已經被觸發器記錄到日誌裏面了數據庫
1 mysql> 2 /*上一篇編寫了觸發器,當修改students表的時候觸發日誌記錄 */ 3 select * from triggerlog; 4 +----+--------------+---------------+-----------------------------------------+ 5 | id | trigger_time | trigger_event | memo | 6 +----+--------------+---------------+-----------------------------------------+ 7 | 1 | after | insert | new student info,id:21 | 8 | 2 | after | update | update student info,id:21 | 9 | 3 | after | update | delete student info,id:21 | 10 | 4 | after | update | from:test2,101.00 to:trigger2,106.00 | 11 | 5 | after | update | from:trigger2,106.00 to:trigger2,107.00 | 12 | 6 | after | update | delete student info,id:11 | 13 | 7 | after | update | from:brand,97.50 to:brand,105.50 | 14 | 8 | after | update | from:helen,96.50 to:helen,98.50 | 15 | 9 | after | update | from:lyn,96.00 to:lyn,97.00 | 16 | 10 | after | update | from:sol,97.00 to:sol,97.00 | 17 | 11 | after | update | from:b1,81.00 to:b1,89.00 | 18 | 12 | after | update | from:b2,82.00 to:b2,90.00 | 19 | 13 | after | update | from:c1,71.00 to:c1,76.00 | 20 | 14 | after | update | from:c2,72.50 to:c2,73.50 | 21 | 15 | after | update | from:A,99.00 to:A,100.00 | 22 +----+--------------+---------------+-----------------------------------------+ 23 15 rows in set
一、遊標用來對查詢結果進行遍歷處理。
二、遊標的使用過程:聲明遊標、打開遊標、遍歷遊標、關閉遊標。
三、遊標主要用在循環處理、存儲過程、函數中使用,用來查詢結果集。
四、遊標的缺點是隻能一行一行操做,在數據量大的狀況下,是不適用的,速度過慢。數據庫大部分是面對集合的,業務會比較複雜,而遊標使用會有死鎖,影響其餘的業務操做,不可取。 當數據量大時,使用遊標會形成內存不足現象。服務器