正好50个循环后,MySQL程序停止错误"错误代码:2014命令不同步;你现在不能运行这个命令"

时间:2015-11-18 13:20:22

标签: mysql mysql-workbench

如果有以下程序,并且在50个循环之后正好停止并出现错误"错误代码:2014命令不同步;你现在不能运行这个命令"。我玩了,似乎没有超时问题。有什么想法吗?

## Prozedur Droppen
DROP PROCEDURE IF EXISTS ifob.uspCreateViewFromTable;

## Prozedur erstellen
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `uspCreateViewFromTable`(IN ViewName varchar(255), IN TableName varchar(255))
BEGIN

  -- DECLARE column varchar(500);
  DECLARE colname varchar(500);
  DECLARE done BOOL DEFAULT FALSE;

  DECLARE column_cursor CURSOR FOR
  SELECT column_name
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = Tablename;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  #View Droppen falls sie schon erstellt wurde
  SET @s = CONCAT('DROP VIEW IF EXISTS ',ViewName);
  PREPARE stmt FROM @s;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
  #Init
  SET @used = 'Used:';
  SET @unused = 'Unused:';
  #Loop through columns
  OPEN column_cursor;
  read_loop: LOOP
      FETCH column_cursor INTO colname;
      SET @c=CONCAT('SELECT COUNT(`',colname, '`) INTO @outvar FROM (SELECT `',colname,'` FROM ',TableName,' WHERE `',colname,'` != 0 OR `',colname,'` != null  ORDER BY `Time` DESC LIMIT 0,10) AS temp');

      PREPARE stmt FROM @c;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;

      IF @outvar>0 THEN
        SET @used = CONCAT(@used,';',colname);
      ELSE
        SET @unused = CONCAT(@unused, ';', colname);
      END IF;
      #debug
      SELECT @used UNION SELECT @unused; 

      IF done THEN
        LEAVE read_loop;
      END IF;
  END LOOP;
  CLOSE column_cursor;

END$$
DELIMITER ;

3 个答案:

答案 0 :(得分:2)

这似乎是早期版本的MySQL工作台的一个问题。升级到MySQL Workbench 6.3.7版本

有一个错误提交https://bugs.mysql.com/bug.php?id=77470

"修复了即将发布的MySQL Workbench 6.3.7版本,以及更改日志条目: Workbench现在将提示继续执行操作 在存储过程中包含循环,而不是仅在之后取消它 约200次执行。

感谢您提供错误报告。"

答案 1 :(得分:1)

据我所知,MySQL存储过程中的循环似乎存在问题,这会限制输出为50的选择数。尝试删除

SELECT @used UNION SELECT @unused; 

然后查看您的程序是否能够运行。选择... into似乎没有这个问题,所以你可以执行你想要的数量。

答案 2 :(得分:0)

我遇到了同样的问题。当我解压缩代码并在Workbench中运行它时 看起来它正在为每个循环调用存储过程。

CREATE PROCEDURE TestLoop()
BEGIN
DECLARE done int DEFAULT FALSE;
DECLARE DetId integer;
DECLARE DetSeq tinyint;
DECLARE ItemId smallint;
DECLARE Cnt smallint DEFAULT 0;
DECLARE Total smallint DEFAULT 0;

DECLARE SaleItem CURSOR FOR SELECT SalDetSalId, SalDetSeq, SalDetInvId FROM SaleDetail WHERE SalDetCost = 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

SELECT COUNT(*) FROM SaleDetail WHERE SalDetCost = 0 INTO Total;

OPEN SaleItem;

read_loop: LOOP
    FETCH SaleItem INTO DetId, DetSeq, ItemId;
    IF done THEN LEAVE read_loop; END IF;
    SET Cnt=Cnt+1;
    SELECT DetId, DetSeq, ItemId, Cnt, Total;
END LOOP read_loop;

CLOSE SaleItem;
SELECT Cnt, Total;
END; //