2016-11-17 5 views
0

なぜ両方の変数が出力されるのですかNULLSELECTカーソルの一部が正しく機能しています。カーソル内のMySQLカーソルがNULLを取り出す

CREATE PROCEDURE p2() 
    BEGIN 
    # Account table 
    DECLARE accountid INT; 
    DECLARE accountname VARCHAR(1000); 

    # 1. cursor finished/done variable comes first 
    DECLARE done INT DEFAULT 0; 
    # 2. the curser declaration and select 
    DECLARE c_account_id_name CURSOR FOR SELECT 
              accountid, 
              accountname 
             FROM temp.test; 
    # 3. the continue handler is defined last 
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE; 

    OPEN c_account_id_name; 
    SET accountid = 0; 
    SET accountname = ''; 

    read_loop: LOOP 
     FETCH c_account_id_name 
     INTO accountid, accountname; 
     IF done 
     THEN 
     LEAVE read_loop; 
     END IF; 
     SELECT accountname; 
    END LOOP; 
    END; 

enter image description here

答えて

2

変数とselect属性を同じにすることはできません...それはMySQLのバグです。 これは

DROP PROCEDURE IF EXISTS p2; 
DELIMITER $$ 
CREATE PROCEDURE p2() 
    BEGIN 
    # Account table 
    DECLARE v_accountidsome INT; #pay attention 
    DECLARE v_accountnameelst VARCHAR(1000); #pay attention 

    # 1. cursor finished/done variable comes first 
    DECLARE v_done INT DEFAULT FALSE; 
    # 2. the cursor declaration and select 
    DECLARE c_account_id_name CURSOR FOR SELECT 
              accountid, #pay attention 
              accountname #pay attention 
             FROM temp.test; 
    # 3. the continue handler is defined last 
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET v_done = TRUE; 

    OPEN c_account_id_name; 

    read_loop: LOOP 
     FETCH c_account_id_name 
     INTO v_accountidsome, v_accountnameelst; 
     IF v_done 
     THEN 
     LEAVE read_loop; 
     END IF; 
     SELECT v_accountidsome; 
     SELECT v_accountnameelst; 
    END LOOP; 
    CLOSE c_account_id_name; 
    END $$ 
DELIMITER ; 

CALL p2(); 

がD.R @節約よりhere

+0

グレート素晴らしい人生を見つけていきます。 !ありがとうございました。 –

関連する問題