我试图创建一个迭代临时表的过程,查询另外4个表中的信息,然后存储在一个或另一个表中。
我确信我的5个表已填充,但循环只发生一次,表中的选择'卷'只返回null,但是当我得到信息并进行查询时,就会有regit。
有人可以告诉我为什么我的循环只返回一次,为什么这个选择返回空?
DROP PROCEDURE IF EXISTS problem_expedition;
DELIMITER |
CREATE PROCEDURE problem_expedition ()
BEGIN
DECLARE t_codSerie varchar (255);
DECLARE t_codProd varchar (255);
DECLARE t_codBatch varchar (255);
DECLARE id_batch bigint (20);
DECLARE id_volume bigint (20);
DECLARE product_id bigint (20);
DECLARE id_historical bigint (20);
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE cursor_p CURSOR FOR SELECT codProd, codBatch, codSerie from volume_confirmed;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN cursor_p;
interno_get: LOOP
FETCH cursor_p INTO t_codProd, t_codBatch, t_codSerie;
IF v_finished = 1 THEN
LEAVE interno_get;
END IF;
SELECT id INTO product_id FROM product WHERE code = t_codProd;
SELECT id INTO id_batch FROM batch WHERE code = t_codLote AND product_id = product_id;
SELECT id into id_volume from volume WHERE volume.serial = t_codSerie and volume.batch_id = id_batch and volume.producto_id = product_id;
SELECT CONCAT ("Searching volume: (", t_codSerie, "-", id_batch, "-", product_id, ").") AS debug;
SET id_historical = 0;
SELECT id INTO id_historical from historical where volume_id = id_volume and (action like 'EXPEDICAO' OR action like 'CONFERENCE');
IF id_historical> 0 THEN
INSERT INTO with_expedition (t_codProd, t_codBatch, t_codSerie, volume_id, historical_id) VALUES (t_codProd, t_codBatch, t_codSerie, volume_id, historical_id);
ELSE
INSERT INTO without_expedition (t_codProd, t_codBatch, t_codSerie, id_volume) VALUES (t_codProd, t_codBatch, t_codSerie, id_volume);
END IF;
END LOOP internal_get;
CLOSE cursor_p;
END |
DELIMITER;
输出
mysql> SELECT * from without_expedition\G
*************************** 1. row ****
t_codProd: CP 3400-H
t_codBatch: O100151787
t_codSerie: 1950
id_volume: NULL
1 row in set (0.00 sec)
mysql> CALL problem_expedition();
+--------------------------------------------+
| debug |
+--------------------------------------------+
- 38196 - 414). |me: (1950
+--------------------------------------------+
1 row in set (0.12 sec)
Query OK, 1 row affected (0.14 sec)
mysql> SELECT id FROM product WHERE code = 'CP 3400-H';
+-----+
| id |
+-----+
| 414 |
+-----+
1 row in set (0.02 sec)
mysql> SELECT id FROM batch WHERE code = 'O100151787' AND produto_id=414;
+-------+
| id |
+-------+
| 38196 |
+-------+
1 row in set (0.02 sec)
mysql> SELECT id from volume WHERE serial like '1950' and batch_id=38196 and product_id=414;
+---------+
| id |
+---------+
| 5910621 |
+---------+
1 row in set (0.08 sec)
如果你查看我的输出,你会看到我有一个id为5910621的卷,但在程序中它只返回null
更新
我把它留在循环中进行了测试,如下所示:
DROP PROCEDURE IF EXISTS problem_expedition;
DELIMITER |
CREATE PROCEDURE problem_expedition ()
BEGIN
DECLARE t_codSerie varchar (255);
DECLARE t_codProd varchar (255);
DECLARE t_codBatch varchar (255);
DECLARE id_batch bigint (20);
DECLARE id_volume bigint (20);
DECLARE product_id bigint (20);
DECLARE id_historical bigint (20);
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE cursor_p CURSOR FOR SELECT codProd, codBatch, codSerie from volume_confirmed;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN cursor_p;
interno_get: LOOP
FETCH cursor_p INTO t_codProd, t_codBatch, t_codSerie;
IF v_finished = 1 THEN
LEAVE interno_get;
END IF;
SELECT id INTO product_id FROM product WHERE code = t_codProd;
SELECT id INTO id_batch FROM batch WHERE code = t_codLote AND product_id = product_id;
SELECT CONCAT ("Searching volume: (", t_codSerie, "-", t_codBatch, "-", t_codProd, ").") AS debug;
END LOOP internal_get;
CLOSE cursor_p;
END |
DELIMITER;
这样循环已经滚动了所有数据,我做了一些其他的测试,实际上错误是在我的卷表中,因为即使我让我的代码完整并将行改为:
SELECT id INTO id_volume FROM volume limit 1
循环只运行一次,我检查了without_expedition表,它有一个有效的音量值。
是否有人知道影响v_finisher或处理程序变量的表中的查询可能发生了什么?
更新2
我在代码中做了以下更改:
DROP PROCEDURE IF EXISTS problem_expedition;
DELIMITER |
CREATE PROCEDURE problem_expedition ()
BEGIN
DECLARE t_codSerie varchar (255);
DECLARE t_codProd varchar (255);
DECLARE t_codLote varchar (255);
DECLARE id_lote bigint (20);
DECLARE id_volume bigint (20);
DECLARE product_id bigint (20);
DECLARE id_historico bigint (20);
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE cursor_volumes CURSOR FOR
SELECT codProd, codlote, codSeries from volume_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN cursor_volumes;
interno_get: LOOP
FETCH cursor_volumes INTO t_codProd, t_codLote, t_codSerie;
IF v_finished = 1 THEN
LEAVE interno_get;
END IF;
SELECT id INTO product_id FROM product WHERE code = t_codProd;
SELECT id INTO id_lote FROM batch WHERE code = t_codLote AND product_id = product_id;
SELECT CONCAT ("Searching volume:", t_codSerie, "-", id_lote, "-", product_id) AS debug;
SELECT id INTO id_volume FROM volume AS v WHERE v.serial like t_codSerie and v.lote_id = id_lote and v.product_id = product_id limit 1;
SET id_historical = 0;
SELECT id INTO historical_id from historical where volume_id = volume_id and action in ('EXPEDITION', 'CONFERENCE') limit 1;
IF id_historical> 0 THEN
INSERT INTO com_expedition (t_codProd, t_codLote, t_codSerie, volume_id, historical_id) VALUES (t_codProd, t_codLote, t_codSerie, volume_id, historical_id);
ELSE
INSERT INTO sem_expedition (t_codProd, t_codLote, t_codSerie, id_volume) VALUES (t_codProd, t_codLote, t_codSerie, id_volume);
END IF;
END LOOP internal_get;
CLOSE cursor_volumes;
END |
DELIMITER;
从我意识到我的循环因为卷表中的查询没有返回任何事实而中断,为什么这种情况似乎与变量t_codSerie
一起发生,以至于它也损坏了{{ 1}}以奇怪的方式返回输出的函数