MySQL:如何手动增加CURSOR FOR中的值?

时间:2014-11-25 02:04:48

标签: mysql

如何手动增加CURSOR FOR的值,例如,我有这个表:

id | invn_id |数量| remaining_qty | trans_date |
1 | 484 | 163 | 163 | 2014-03-21 |
2 | 230 | 100 | 100 | 2014-03-21 |
3 | 412 | 150 | 150 | 2014-03-21 |

我想首先获得第一行的remaining_qty,然后循环它直到它成为0。当它变为0时,我将前往remaining_qty等于100的下一行。但问题是我无法在CURSOR FOR中执行此过程,因为它已经FETCH内部的所有行。是否有可能手动增加CURSOR FOR内的值?到目前为止,我有这段代码:

DELIMITER //
CREATE PROCEDURE sample ()
BEGIN
   DECLARE invn_id, row_count, first_col, second_col VARCHAR(50);
   DECLARE final_count INT;
   DECLARE col CURSOR FOR SELECT remaining_quantity, invn.trans_date
            FROM invn
            INNER JOIN prod p ON p.id=invn.prod_id
            INNER JOIN bucket_ins bi ON bi.inventory_id=invn.id
            WHERE p.id=(
                SELECT p.id
                FROM invn
                INNER JOIN prod p ON p.id=invn.prod_id
                INNER JOIN bucket_out bo ON bo.inventory_id=invn.id
                WHERE invn.id=406 LIMIT 1
            )
            ORDER BY p.id, invn.trans_date ASC;

   SELECT COUNT(*) INTO row_count
   FROM invn
   INNER JOIN prod p ON p.id=invn.prod_id
   INNER JOIN bucket_ins bi ON bi.inventory_id=invn.id
   WHERE p.id=(
      SELECT p.id
      FROM invn
      INNER JOIN prod p ON p.id=invn.prod_id
      INNER JOIN bucket_out bo ON bo.inventory_id=invn.id
      WHERE invn.id=406 LIMIT 1
    )
    ORDER BY p.id, invn.trans_date ASC;

    OPEN col;
    read_loop : WHILE row_count != 0 DO
    FETCH col INTO first_col, second_col;
       IF first_col <> 0 THEN
          SET first_col = first_col - 1;
          SELECT first_col , second_col;
       ELSEIF first_col = 0 THEN
          SET row_count = row_count - 1;
       END IF;
    END WHILE read_loop;
    CLOSE col;

END //
DELIMITER ; 

谢谢!

0 个答案:

没有答案