mysql 动态游标
这是我编写的程序 - Cursors c1
& c2
。 c2
在 c1
内部,我尝试在 c1
下面声明 c2
(在 c1
之外)光标),但随后 I
没有采用更新后的值:(任何使其正常工作的建议都会有所帮助,谢谢
create table t1(i int);
create table t2(i int, j int);
insert into t1(i) values(1), (2), (3), (4), (5);
insert into t2(i, j) values(1, 6), (2, 7), (3, 8), (4, 9), (5, 10);
delimiter $
CREATE PROCEDURE p1()
BEGIN
DECLARE I INT;
DECLARE J INT;
DECLARE done INT DEFAULT 0;
DECLARE c1 CURSOR FOR SELECT i FROM t1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN c1;
REPEAT
FETCH c1 INTO I;
IF NOT done THEN
select I;
DECLARE c2 CURSOR FOR
SELECT j FROM t2 WHERE i = I;
OPEN c2;
REPEAT
FETCH c2 into J;
IF NOT done THEN
SELECT J;
END IF;
UNTIL done END REPEAT;
CLOSE c2;
set done = 0;
END IF;
UNTIL done END REPEAT;
CLOSE c1;
END$
delimiter ;
Here is the procedure I wrote- Cursors c1
& c2
. c2
is inside c1
, I tried declaring c2
below c1
(outside the c1
cursor) but then I
is NOT taking the updated value :( Any suggestions to make it working would be helpful, Thanks
create table t1(i int);
create table t2(i int, j int);
insert into t1(i) values(1), (2), (3), (4), (5);
insert into t2(i, j) values(1, 6), (2, 7), (3, 8), (4, 9), (5, 10);
delimiter $
CREATE PROCEDURE p1()
BEGIN
DECLARE I INT;
DECLARE J INT;
DECLARE done INT DEFAULT 0;
DECLARE c1 CURSOR FOR SELECT i FROM t1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN c1;
REPEAT
FETCH c1 INTO I;
IF NOT done THEN
select I;
DECLARE c2 CURSOR FOR
SELECT j FROM t2 WHERE i = I;
OPEN c2;
REPEAT
FETCH c2 into J;
IF NOT done THEN
SELECT J;
END IF;
UNTIL done END REPEAT;
CLOSE c2;
set done = 0;
END IF;
UNTIL done END REPEAT;
CLOSE c1;
END$
delimiter ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白你想做什么。当您在过程中一次选择一个字段时,您将创建多个结果集。我怀疑这就是你想要的。
以下过程要简单得多,并在单个结果集中为您提供相同的数据:
也许您可以编辑您的问题并描述您想要的结果,或者显示所需输出的示例。
I don't understand what you are trying to do. When you select individual fields one at a time in a procedure, you're creating multiple result sets. I doubt that's what you want.
The following procedure is far simpler and gives you the same data in a single result set:
Perhaps you could edit your question and describe what result you want, or show an example of the desired output.