MySQL脚本中IF和Loop的正确语法是什么(没有存储过程)

时间:2017-07-14 10:17:07

标签: mysql

我们希望将旧MySQL数据库中的数据迁移到具有其他结构的新数据库中。 所以我们开始用一些SQL语句编写一个SQL脚本(没有存储过程!)。

如果有要迁移的条目,我们正在查看旧数据库的一个表。 如果我们找到了条目,我们想要通过它们循环并将它们插入到新数据库的表中。

但在MySQL中,我们收到错误消息,IF在当前位置无效,并且循环使用BEGIN和END也不能在mySQL中工作。

这是我们想要做的一个例子:

IF (SELECT COUNT(`AbhId`) FROM `p16altdatentest`.`abhaengigkeit` > 0) THEN
  SELECT myAbhId = min(`AbhId`) FROM `p16altdatentest`.`abhaengigkeit`;

  -- Go over each myAbhId
      WHILE myAbhId > 0
      BEGIN
        INSERT INTO `paragraph16`.`description`
          (`languageId`,`textKey`,`text`,`isDeleted`,`changedBy`,`changeDate`)
          SELECT 1, CONCAT('Dependancy_', REPLACE(`p16altdatentest`.`abhaengigkeit`.`AbhId`, '_', '')), `p16altdatentest`.`abhaengigkeit`.`Text_D`, 0, NULL, CURRENT_TIMESTAMP
          FROM `p16altdatentest`.`abhaengigkeit`
          WHERE `p16altdatentest`.`abhaengigkeit`.`AbhId` = myAbhId;
        INSERT INTO `paragraph16`.`description`
          (`languageId`,`textKey`,`text`,`isDeleted`,`changedBy`,`changeDate`)
          SELECT 2, CONCAT('Dependancy_', REPLACE(`p16altdatentest`.`abhaengigkeit`.`AbhId`, '_', '')), `p16altdatentest`.`abhaengigkeit`.`Text_E`, 0, NULL, CURRENT_TIMESTAMP
          FROM `p16altdatentest`.`abhaengigkeit`
          WHERE `p16altdatentest`.`abhaengigkeit`.`AbhId` = myAbhId;
      END
END IF

0 个答案:

没有答案