1. 程式人生 > >mysql儲存過程 指標迴圈

mysql儲存過程 指標迴圈

CREATE PROCEDURE `tp1`()

BEGIN

//變數

DECLARE aid VARCHAR(50) ;

DECLARE stop INT DEFAULT 0;

//指標

DECLARE cur CURSOR FOR (SELECT id as aid from s_assetclassify);

//讀取不到時改變stop,根據stop判斷是否迴圈

declare CONTINUE HANDLER FOR SQLSTATE '02000' SET stop = null;

OPEN cur;
FETCH cur INTO aid;
WHILE ( stop is not null) DO
INSERT INTO table1 (name1,name2,name3)VALUES(aid,1,1);
FETCH cur INTO aid;
END WHILE;
close cur;
end