MySQL 中嵌套循环中的多个游标
我想做一些在 MySQL 中显得有点复杂的事情。 事实上,我希望打开一个游标,执行一个循环,并在这个循环中,使用上一个要执行的提取中的数据打开第二个游标,并重新循环结果。
DECLARE idind INT;
DECLARE idcrit INT;
DECLARE idindid INT;
DECLARE done INT DEFAULT 0;
DECLARE done2 INT DEFAULT 0;
DECLARE curIndicateur CURSOR FOR SELECT id_indicateur FROM indicateur;
DECLARE curCritereIndicateur CURSOR FOR SELECT C.id_critere FROM critere C where C.id_indicateur=idind;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
set idindid=54;
OPEN curIndicateur;
REPEAT
FETCH curIndicateur INTO idind;
open curCritereIndicateur;
REPEAT
FETCH curIndicateur INTO idcrit;
INSERT INTO SLA_DEMANDE_STATUS (iddemande,idindicateur,indicateur_status,progression) values('0009',idcrit,'OK',10.0);
UNTIL done END REPEAT;
close curCritereIndicateur;
UNTIL done END REPEAT;
CLOSE curIndicateur;
事实上,由于只能为 SQLSTATE 声明一个处理程序,因此如何对两个游标以不同的方式执行“直到完成”操作? 如果第一个结束,第二个也结束。
I wish to do something which appear a bit complicated in MySQL.
In fact, I wish to open a cursor, do a loop, and in this loop, open a second cursor using the data from the previous fetch to be executed, and re-loop on the results.
DECLARE idind INT;
DECLARE idcrit INT;
DECLARE idindid INT;
DECLARE done INT DEFAULT 0;
DECLARE done2 INT DEFAULT 0;
DECLARE curIndicateur CURSOR FOR SELECT id_indicateur FROM indicateur;
DECLARE curCritereIndicateur CURSOR FOR SELECT C.id_critere FROM critere C where C.id_indicateur=idind;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
set idindid=54;
OPEN curIndicateur;
REPEAT
FETCH curIndicateur INTO idind;
open curCritereIndicateur;
REPEAT
FETCH curIndicateur INTO idcrit;
INSERT INTO SLA_DEMANDE_STATUS (iddemande,idindicateur,indicateur_status,progression) values('0009',idcrit,'OK',10.0);
UNTIL done END REPEAT;
close curCritereIndicateur;
UNTIL done END REPEAT;
CLOSE curIndicateur;
In fact, how to do 'Until done' differently for the two cursors, because you can only declare one handler for SQLSTATE?
If the first ends, the second ends too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在第一个游标循环内定义一个新的块,并在该块中使用不同的声明。
像这样的东西:
You need to define a new BLOCK inside your 1st cursor loop and use different Declares in that block.
Something like:
如果我错了,请纠正我,但看起来您想要做的是将记录批量插入到“SLA_DEMANDE_STATUS”表中。包括找到的每个指标的每个条件,并根据每个指标的条件 ID 默认使用“0009”、“OK”和 10.0 值。
这一切都可以在单个 SQL-Insert 中完成。 INSERT INTO ... from a SQL-Select...
现在,如果您只想包含单个“id_indicateur”条目,您可以将其添加到 select 语句的 WHERE 子句中。
请注意,我的 SQL-Select 强制值与您想要填充的列相对应。它们都将以相同的名称插入到目标表中。这样做的好处是,您只需运行 SQL-SELECT 部分即可查看您期望插入的数据...如果它不正确,您可以调整它以适应您想要的任何限制。
Correct me if I'm wrong, but it looks like what you are trying to do is a bulk insert of records into your "SLA_DEMANDE_STATUS" table. Include every criteria for every indicator found and default it with the values of '0009', 'OK' and 10.0 per each indicator's Criteria ID.
This can all be done in a single SQL-Insert. INSERT INTO ... from a SQL-Select...
Now, if you want to only include a single "id_indicateur" entry, you can add that to the WHERE clause of the select statement.
Notice that my SQL-Select has forced values to correspond with the columns you want to have populated. They will all be inserted to the destination table by the same name. The nice thing about this, you can just run the SQL-SELECT portion just to see the data you would EXPECT to have inserted... if its incorrect, you can adjust it to fit whatever restrictions you want.
您可以使用循环,并重置句柄的值,如下所示:
you could use loop,and reset the value of the handle,like this:
或者重新定义CONTINUE HANDLE:
似乎循环内的所有select语句都执行CONTINUE HANDLE
Or redefine the CONTINUE HANDLE:
It seems that all select statements inside a loop execute the CONTINUE HANDLE