2

我是使用 mysql 存储过程的新手。有错误我无法修复这是我的代码

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”

请帮我!!

4

1 回答 1

2
  1. 必须声明一个变量 no_more_rows,在过程主体的开头写下一行。

    声明 no_more_rows INT DEFAULT 0;

  2. 等号运算符是 '='

    IF productCd_val == 'SAV' 等应该是 IF productCd_val = 'SAV'

  3. 使用 SET 命令分配值,例如 -

    设置 balance_val = balance_val - 10;

于 2011-04-29T05:42:43.030 回答