【问题标题】:how to read data from CURSOR - mysql stored procedure如何从 CURSOR 读取数据 - mysql 存储过程
【发布时间】:2018-05-12 05:33:18
【问题描述】:

我已经在 mysql 存储过程中实现了 CURSOR...我在读取游标时遇到了挑战...基本上我想从游标中读取列值并将其存储到变量中..但我得到了变量值为NULL...解决方案是什么...请帮助我..提前致谢...

我的代码如下,

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 无效的 空

因为我的购物车表中有一条记录...

【问题讨论】:

    标签: mysql stored-procedures database-cursor


    【解决方案1】:

    C.1 Restrictions on Stored Programs :: Name Conflicts within Stored Routines

    选项 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);
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多