1. 程式人生 > >MySql中DECLARE CONTINUE HANDLER FOR NOT FOUND 解釋

MySql中DECLARE CONTINUE HANDLER FOR NOT FOUND 解釋

轉載地址:http://blog.csdn.net/tengdazhang770960436/article/details/12712143

1.解釋:

MySQL的儲存過程中經常會看到這句話:DECLARE CONTINUE HANDLER FOR NOT FOUND。

它的含義是:若沒有資料返回,程式繼續,並將變數IS_FOUND設為0 ,這種情況是出現在select XX into XXX from tablename的時候發生的。

2.示例:

/*建立儲存過*/CREATEPROCEDURE useCursor()
BEGIN/*區域性變數的定義*/declare tmpName varchar(20)default

'' ;
declare allName varchar(255)default'' ;

declare cur1 CURSORFORSELECT name FROM test.level ;
declareCONTINUE HANDLER FOR SQLSTATE '02000'SET tmpname =null;
#也可以這麼寫

#DECLARE CONTINUE HANDLER FOR NOT FOUNDSET tmpname =null;
OPEN cur1;


FETCH cur1 INTO tmpName;    WHILE ( tmpname is notnull) DO
    set tmpName

= CONCAT(tmpName ,";") ;
    set allName = CONCAT(allName ,tmpName) ;    FETCH cur1 INTO tmpName;
ENDWHILE;

CLOSE cur1;

select allName ;
END;
call useCursor()