mysql 动态游标

发布于 2024-08-18 10:20:04 字数 1051 浏览 9 评论 0原文

这是我编写的程序 - Cursors c1 & c2c2c1 内部,我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2024-08-25 10:20:04

我不明白你想做什么。当您在过程中一次选择一个字段时,您将创建多个结果集。我怀疑这就是你想要的。

以下过程要简单得多,并在单个结果集中为您提供相同的数据:

CREATE PROCEDURE p1()
BEGIN
  SELECT i, j FROM t1 JOIN t2 USING (i);
END

也许您可以编辑您的问题并描述您想要的结果,或者显示所需输出的示例。

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:

CREATE PROCEDURE p1()
BEGIN
  SELECT i, j FROM t1 JOIN t2 USING (i);
END

Perhaps you could edit your question and describe what result you want, or show an example of the desired output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文