Nested Cursor Declare Issue Mysql

岁酱吖の 提交于 2019-12-25 01:28:33

问题


I'm trying to make a Nested Cursor in Mysql by following this instruction.
Then i got this issue:

#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

I've 2 table 'account' and 'n_activity' (n = account_id in table 'account')
Ex: i've table 'account' and '20_activity'.
So i want to loop the 'account_id' and get the 'activity_id' from that loop.



Here is my code:

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();

Please help, thanks.

来源:https://stackoverflow.com/questions/57830576/nested-cursor-declare-issue-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!