mysql - mysql 中的游标错误语法

标签 mysql

我有这样的程序,但我一直想执行它,我在声明变量附近遇到语法错误...任何人都可以告诉我我做错了什么

我有两张表,一张用于问题,一张用于答案,还有用于将数据迁移到该表的 Excel 文件。 Excel 中的数据如下所示:

ID      N       TITLE
3       99500   question1
4       
5               answer1
6       X       answer2
7               answer3




 DELIMITER $$
create Procedure proc_answermigration()
begin

  DECLARE @i,@n,@q,@ind,@pt varchar default '';
  DECLARE @done,@aord int default 0;
  DECLARE cur cursor for select ID, N, question, from test.qustionmigration;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET @done = 1;
  open cur;

  read_loop: Loop
  fetch cur into i,n,q;

  IF done THEN
  LEAVE read_loop;
  END IF;

  if n <> '' or n <> 'X'
    then
      select @ind = iq.question_id, @pt = iq.points from test.qustionmigration qm
      inner join ilias.qpl_questions iq on qm.question = iq.question_text
      where question = q
  else
      insert into qpl_a_sc
      (
        answer_id,
        question_fi,
        answertext,
        points,
        aorder,
        tstamp
      )
      select (select sequence from qpl_a_sc_seq),
              ind,
              question,
              pt,
              aord,
              '1342884200'
      from test.qustionmigration
      end if;

  update qpl_a_sc_seq
  set sequence = sequence + 1;

  if @aord = 0 then set @aord = 1;
  elseif @aord = 1 then set @aord = 2;
  else set @aord = 0;

  end loop;
  CLOSE cur;

end$$

DELIMITER ;

我更正了一些语句,但仍然有语法错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LOOP; close cur; end' at line 52


DELIMITER $$
create Procedure proc_answermigration()
begin

  DECLARE i,n,q,ind,pt varchar(500) default '';
  DECLARE done,aord,c int default 0;
  DECLARE cur cursor for select ID, N, question from test.qustionmigration;
  DECLARE  CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  open cur;


  read_loop: LOOP
    fetch cur into i,n,q;
    IF n <> '' or n <> 'X'
      then
        select ind = iq.question_id, pt = iq.points from test.qustionmigration qm
        inner join ilias.qpl_questions iq on qm.question = iq.question_text
        where question = q;
    else
        insert into qpl_a_sc
        (
          answer_id,
          question_fi,
          answertext,
          points,
          aorder,
          tstamp
        )
        select (select sequence from qpl_a_sc_seq),
                ind,
                question,
                pt,
                aord,
                '1342884200'
        from test.qustionmigration;
    end IF;

    update qpl_a_sc_seq
    set sequence = sequence + 1;

    if aord = 0 then set aord = 1;
    elseif aord = 1 then set aord = 2;
    else set aord = 0;

    set c = c + 1;


  IF done = 1 THEN
      LEAVE read_loop;
  END IF;

  END LOOP;

  close cur;
end$$
DELIMITER ;

最佳答案

您的最后一个 IF ELSE block 缺少 END IF:

if @aord = 0 then set @aord = 1;
elseif @aord = 1 then set @aord = 2;
else set @aord = 0;
/* END IF either belongs here or after following statements, depending on your intended logic */
/* Either way, this block is unclosed when you close the loop */
end if

当解析器读取END LOOP;时,它会寻找END IF,并在LOOP处报告语法错误。

关于mysql - mysql 中的游标错误语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599304/

相关文章:

php - MySql 添加新记录时触发错误

php - 如何从函数调用变量并在回显中回显它

php - 在 mysqli 和 php 中与表 2 连接时从表 1 中获取不同的记录

php - 如何使用 PHP 更新 mysql 中的一行数据?

c++ - 将 C++ 与 MySql 一起使用 : segmentation fault

php - 偏移量 0 对 MySQL 结果索引 10 无效,无法从 mysql php 检索数据

mysql - 错误代码 1292 截断不正确的 DOUBLE 值 : 'AIN'

mysql - 使用mysql连接的问题

mysql - 一次填充MySQL记录一对多关联表

Mysql InnoDB 表已锁定,但我可以从另一个 session 中获取 "select"。是什么赋予了?