以下是一個預存程序,它從具有以下資料的表「student_info」的名稱列中取得記錄-
mysql> Select * from Student_info; +-----+---------+------------+------------+ | id | Name | Address | Subject | +-----+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Chandigarh | Literature | | 125 | Raman | Shimla | Computers | | 127 | Ram | Jhansi | Computers | +-----+---------+------------+------------+ 4 rows in set (0.00 sec) mysql> Delimiter // mysql> CREATE PROCEDURE cursor_defined(OUT val VARCHAR(20)) -> BEGIN -> DECLARE a,b VARCHAR(20); -> DECLARE cur_1 CURSOR for SELECT Name from student_info; -> DECLARE CONTINUE HANDLER FOR NOT FOUND -> SET b = 1; -> OPEN CUR_1; -> REPEAT -> FETCH CUR_1 INTO a; -> UNTIL b = 1 -> END REPEAT; -> CLOSE CUR_1; -> SET val = a; -> END// Query OK, 0 rows affected (0.04 sec) mysql> Delimiter ; mysql> Call cursor_defined2(@val); Query OK, 0 rows affected (0.11 sec) mysql> Select @val; +------+ | @val | +------+ | Ram | +------+ 1 row in set (0.00 sec)
從上面的結果集中,我們可以看到val 參數的值是“Ram”,因為它是“Name”列的最後一個值。
以上是建立一個 MySQL 預存程序,使用遊標從表中取得行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!