基於MySQL遊標的具體使用詳解

來源:互聯網
上載者:User

測試表 level ;

複製代碼 代碼如下:create table test.level (name varchar(20));

再 insert 些資料 ;

代碼

初始化

複製代碼 代碼如下:drop procedure if exists useCursor //

建立 預存程序 create複製代碼 代碼如下:CREATE PROCEDURE useCursor()

BEGIN

局部變數的定義 declare複製代碼 代碼如下:declare tmpName varchar(20) default '' ;
declare allName varchar(255) default '' ;
declare cur1 CURSOR FOR SELECT name FROM test.level ;

MySQL 遊標 異常後 捕捉

並設定 迴圈使用 變數 tmpname 為 null 跳出迴圈。

複製代碼 代碼如下:declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tmpname = null;

開遊標複製代碼 代碼如下:OPEN cur1;

遊標向下走一步複製代碼 代碼如下:FETCH cur1 INTO tmpName;

迴圈體 這很明顯 把MySQL 遊標查詢出的 name 都加起並用 ; 號隔開複製代碼 代碼如下:WHILE ( tmpname is not null) DO
set tmpName = CONCAT(tmpName ,";") ;
set allName = CONCAT(allName ,tmpName) ;

遊標向下走一步複製代碼 代碼如下:FETCH cur1 INTO tmpName;

結束迴圈體: 複製代碼 代碼如下:END WHILE;

關閉遊標複製代碼 代碼如下:CLOSE cur1;

選擇資料複製代碼 代碼如下:select allName ;

結束預存程序複製代碼 代碼如下:END;//

調用預存程序:複製代碼 代碼如下:call useCursor()//

運行結果:複製代碼 代碼如下:mysql> call useCursor()//

+--------------------------------------+

| allName |

+--------------------------------------+

| f1;c3;c6;c5;c2;c4;c1;f1;f3;f4;f2;f5; |

+--------------------------------------+

1 row in set (0.00 sec)

loop迴圈遊標:複製代碼 代碼如下:DELIMITER $$

DROP PROCEDURE IF EXITS cursor_example$$
CREATE PROCEDURE cursor_example()
READS SQL DATA
BEGIN
DECLARE l_employee_id INT;
DECLARE l_salary NUMERIC(8,2);
DECLARE l_department_id INT;
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT employee_id, salary, department_id FROM employees;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN cur1;
emp_loop: LOOP
FETCH cur1 INTO l_employee_id, l_salary, l_department_id;
IF done=1 THEN
LEAVE emp_loop;
END IF;
END LOOP emp_loop;
CLOSE cur1;
END$$
DELIMITER ;

repeat迴圈遊標:複製代碼 代碼如下:/*建立過程*/
DELIMITER //
DROP PROCEDURE IF EXISTS test //
CREATE PROCEDURE test()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a VARCHAR(200) DEFAULT '';
DECLARE c VARCHAR(200) DEFAULT '';

DECLARE mycursor CURSOR FOR SELECT fusername FROM uchome_friend;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN mycursor;

REPEAT
FETCH mycursor INTO a;
IF NOT done THEN
SET c=CONCAT(c,a);/*字串相加*/
END IF;

UNTIL done END REPEAT;

CLOSE mycursor;

SELECT c;
END //
DELIMITER ;

複製代碼 代碼如下:/*建立過程*/
DELIMITER //
DROP PROCEDURE IF EXISTS test //
CREATE PROCEDURE test()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a VARCHAR(200) DEFAULT '';
DECLARE c VARCHAR(200) DEFAULT '';

DECLARE mycursor CURSOR FOR SELECT fusername FROM uchome_friend;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN mycursor;

REPEAT
FETCH mycursor INTO a;
IF NOT done THEN
SET c=CONCAT(c,a);/*字串相加*/
END IF;

UNTIL done END REPEAT;

CLOSE mycursor;

SELECT c;
END //
DELIMITER ;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.