如何从 CURSOR - mysql 存储过程中读取数据
how to read data from CURSOR - mysql stored procedure
我已经在 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
无效的
空
因为我的购物车中有一条记录 table...
参见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);
...
我已经在 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 无效的 空
因为我的购物车中有一条记录 table...
参见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);
...