如何纠正MariaDB程序中的错误#1064?

gz5pxeao  于 2023-02-12  发布在  其他
关注(0)|答案(1)|浏览(144)
DELIMITER $$

CREATE PROCEDURE insert_priority_rows()
BEGIN
  DECLARE max_heuristics INT DEFAULT 10;
  DECLARE heuristic_ID INT;

  DECLARE cur CURSOR FOR
    SELECT heuristicID
    FROM heuristics;
    
  DECLARE CONTINUE HANDLER 
  FOR NOT FOUND SET heuristic_ID = 0;
    
  DECLARE @study_ID INT;

  SELECT MAX(studyID) INTO @study_ID FROM study;

  OPEN cur;

  REPEAT
    FETCH cur INTO heuristic_ID;
    IF heuristic_ID = 0 THEN
      LEAVE;
    END IF;

    INSERT INTO heuristic_priority (studyID, heuristicID, h_priority)
    VALUES (@study_ID, heuristic_ID, 'm');
  UNTIL heuristicID = 0 END REPEAT;

  CLOSE cur;
END$$

DELIMITER ;

它抛出如下错误:

1064-SQL语法中有错误;检查与您的MariaDB服务器版本对应的手册,以获得在"@study_ID INT "附近使用的正确语法;

我应该做些什么来纠正这个问题?

ldxq2e6h

ldxq2e6h1#

你不需要大部分的代码,循环可以用下面的查询来代替:

DELIMITER $$

CREATE PROCEDURE insert_priority_rows()
BEGIN
    INSERT INTO heuristic_priority (studyID, heuristicID, h_priority)
    SELECT m.max_study_id, h.heuristicID, 'm'
    FROM heuristics AS h
    CROSS JOIN (SELECT MAX(studyID) as max_study_id FROM study) AS m;
END$$

相关问题