mysql存储过程中读取数据

h43kikqp  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(400)

我已经在mysql存储过程中实现了游标。。。我在阅读光标时遇到了挑战。。。基本上我想从游标读取列值并将其存储到变量中。。但是我得到的变量值是空的。。。解决办法是什么。。。请帮帮我。。提前谢谢。。。
我的代码如下,

delimiter //

CREATE PROCEDURE placeOrder(IN cartId INT)
BEGIN

   DECLARE countitem INT DEFAULT 0;
   DECLARE productId INT;
   DECLARE quantity INT;
   DECLARE itemDicountAmt DOUBLE DEFAULT 0;
  DECLARE v_finished INT DEFAULT 0;

 DEClARE cart_cursor CURSOR FOR 
 SELECT ProductId, Quantity FROM TBL_SHOPPING_CART_ITEM;

 -- declare NOT FOUND handler
 DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET v_finished = 1;

 OPEN cart_cursor;

 get_cart: LOOP

 FETCH cart_cursor into productId, quantity;

 IF v_finished = 1 THEN 
 LEAVE get_cart;
 END IF;

 -- build email list
 insert into debugtable select concat('PRODUCT :', productId);
  insert into debugtable select concat('QUANTITY :', quantity);
 END LOOP get_cart;

 CLOSE cart_cursor;

END//

delimiter ;

输出:
tmptable空
因为我有一个记录在我的购物车表。。。

cygmwpex

cygmwpex1#

请参阅c.1存储程序限制::存储例程中的名称冲突。
方案1:

...
/*DEClARE cart_cursor CURSOR FOR 
 SELECT ProductId, Quantity FROM TBL_SHOPPING_CART_ITEM;*/
DEClARE cart_cursor CURSOR FOR 
 SELECT
   TBL_SHOPPING_CART_ITEM.ProductId,
   TBL_SHOPPING_CART_ITEM.Quantity
 FROM TBL_SHOPPING_CART_ITEM;
...

方案2:

...
/*DECLARE productId INT;
DECLARE quantity INT;*/
DECLARE _productId INT;
DECLARE _quantity INT;
...
/*FETCH cart_cursor into productId, quantity;*/
FETCH cart_cursor into _productId, _quantity;
...
/*insert into debugtable select concat('PRODUCT :', productId);
insert into debugtable select concat('QUANTITY :', quantity);*/
insert into debugtable select concat('PRODUCT :', _productId);
insert into debugtable select concat('QUANTITY :', _quantity);
...

相关问题