首页 > 解决方案 > 嵌套游标声明问题 Mysql

问题描述

我正在尝试按照此指令在 Mysql 中创建一个嵌套游标。
然后我得到了这个问题:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DECLARE activityids CURSOR FOR SELECT activity_id FROM @_activity;
END BLOCK2;' at line 22

我有 2 个表“帐户”和“n_activity”(表“帐户”中的 n = account_id)
例如:我有表“帐户”和“20_activity”。
所以我想循环“account_id”并从该循环中获取“activity_id”。 这是我的代码:

在此处输入图像描述 在此处输入图像描述

DROP PROCEDURE if exists update_schema_activity_startdate_and_duedate;
DELIMITER $$
CREATE PROCEDURE update_schema_activity_startdate_and_duedate()
BEGIN

  DECLARE done INT DEFAULT FALSE;
  DECLARE accountid INT;  
  --
  -- GET ALL ACCOUNT ID
  --

  DECLARE accountids CURSOR FOR SELECT account_id FROM account;

  --
  -- LOOP 
  --  

  OPEN accountids; 
  read_loop: LOOP 
    FETCH accountids INTO accountid;

    BLOCK2: BEGIN
        SET @_activity = CONCAT(accountid,'_activity');
        DECLARE activityids CURSOR FOR SELECT activity_id FROM @_activity;
    END BLOCK2;

  END LOOP; 
  CLOSE accountids;
END$$
DELIMITER ;
CALL update_schema_activity_startdate_and_duedate();

请帮忙,谢谢。

标签: mysqlnestedcursor

解决方案


推荐阅读