2011-04-29 75 views
2

我是使用mysql存儲過程的新手。有錯誤,我無法修復 這是我的代碼在Mysql存儲過程中的未知系統變量「no_more_data」

DELIMITER $$ 

CREATE PROCEDURE `bankdb`.`charge`() 
BEGIN 
DECLARE idcust_val INT; 
DECLARE balance_val FLOAT; 
DECLARE productCd_val VARCHAR(10); 

DECLARE loop_cntr INT DEFAULT 0; 
DECLARE num_rows INT DEFAULT 0; 

DECLARE col_cur CURSOR FOR 
select c.cust_id, c.balance, c.product_cd 
from account c; 
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE; 

OPEN col_cur; 
select FOUND_ROWS() into num_rows; 

read_loop: LOOP 
FETCH col_cur INTO idcust_val, balance_val, productCd_val; 

IF no_more_rows THEN 
close col_cur; 
leave read_loop; 
END IF; 

IF productCd_val == 'SAV' || productCd_val == 'CD' THEN 
    IF balance_val == 2000 THEN 
      balance_val = balance_val-10; 
      UPDATE account SET avail_balance = balance_val 
      WHERE account_id =idcust_val; 
    END IF; 
END IF; 

IF productCd_val == 'CHK' || productCd_val == 'SAV' || productCd_val == 'MM' 
|| productCd_val == 'CD' THEN 
    balance_val = balance_val + (balance_val*0,05); 
    UPDATE account SET avail_balance = balance_val 
    WHERE account_id =idcust_val; 
ELSE 
    balance_val = balance_val - (balance_val*0,1); 
    UPDATE account SET avail_balance = balance_val 
    WHERE account_id =idcust_val; 
END IF; 
SET loop_cntr = loop_cntr + 1; 
END LOOP; 
END $$ 

DELIMITER ; 

當我執行它,MySQL查詢瀏覽器顯示的錯誤是這樣的:

腳本行:3未知的系統變量「no_more_rows」

請幫助我!

回答

2
  1. 一個變量no_more_rows必須被拖延,在程序主體開始時寫下一行。

    DECLARE no_more_rows INT DEFAULT 0;

  2. 等於運算符是 '='

    IF productCd_val == 'SAV' 和等應IF productCd_val = 'SAV'

  3. 使用SET指令到指定值,例如 -

    SET balance_val = balance_val - 10;

+0

很好的回答,謝謝 – Frenda 2011-04-29 06:15:22

+0

儘量避免使用遊標。 – Devart 2011-04-29 06:19:10

+0

它仍然是這樣的錯誤 - >操作數應該包含1列,爲什麼? – Frenda 2011-04-29 08:53:05