如何在MySQL中的异常后继续执行存储过程?

时间:2015-03-11 18:28:30

标签: mysql

我正在运行一个存储过程来填充一个包含来自另一个表的随机记录的新表。但是,目标表为PK以避免重复值。因此,存储过程在找到重复条目时停止。如何在每次停止时不从命令行手动执行的情况下继续执行?

的MySQL>调用random_while(); ERROR 1062(23000):重复输入' 1039499'关键' PRIMARY'

    CREATE DEFINER=`root`@`localhost` PROCEDURE `random_while`()
BEGIN
  DECLARE v1 INT DEFAULT 5500;
  /*DECLARE EXIT HANDLER FOR 1062 go to label continue;*/

  /*label:label continue;*/
  WHILE v1 > 0 DO
    INSERT INTO `random_pull_request`
    SELECT t2.* FROM (SELECT name FROM project_pull_request ppr, 
                                  (SELECT RAND() * (SELECT MAX(id_project)-1 
                                   FROM project_pull_request) AS tid) AS tmp
                                   WHERE ppr.id_project >= tmp.tid
                                   ORDER BY id_project ASC
                                   LIMIT 1) AS id_random_project, pull_request_class_merged t2
    WHERE id_random_project.name = t2.project_name  
    order by rand() LIMIT 1;
    SET v1 = v1 - 1;
  END WHILE;
END

感谢您的帮助。

亲切的问候

0 个答案:

没有答案