使用LOOP的MySQL程序

时间:2017-07-20 10:29:36

标签: mysql procedure

在我的程序中,我真的在努力解决这个问题。我无法理解错误的含义。我猜任何人都可以尝试在不知道数据库的情况下运行它...

DROP PROCEDURE IF EXISTS removeDuplicates;
DELIMITER $$
CREATE PROCEDURE removeDuplicates(str TEXT) 
BEGIN
    DECLARE temp_word TEXT;
    DECLARE last_word TEXT;
    DECLARE result TEXT;
    DECLARE finished INT DEFAULT false;
    DECLARE words_cursor CURSOR FOR
        SELECT word FROM explosion;
    DECLARE CONTINUE handler FOR NOT found
        SET finished = true;

    CALL explode(str);
    DROP TABLE IF EXISTS temp_words;
    CREATE TABLE temp_words (id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, t VARCHAR(100));

    OPEN words_cursor;
    loop_words: LOOP
        FETCH words_cursor INTO temp_word;
        IF finished THEN
            LEAVE loop_words;
        IF temp_word = "" THEN
            SET result = CONCAT(result, temp_word, " ");
            SET last_word = temp_word;
        ELSEIF last_word = temp_word THEN
            SET last_word = temp_word;
        ELSE 
            INSERT INTO temp_words (t) VALUES (temp_word);
        END IF;
    END LOOP loop_words;
    CLOSE words_cursor;
    RETURN result;
END$$

DELIMITER ;

任何指针都会感激不尽。我得到的错误是:

  

[查询2中的错误]您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在&LOCH loop_words附近使用正确的语法;       关闭words_cursor;       返回结果;   END'第29行

我确定它一定很简单,但是如果我能解决的话,我会被诅咒。

谢谢,克里斯

1 个答案:

答案 0 :(得分:1)

/[Aa]n?( \w+)? shame(*SKIP)(*FAIL)|shame/

你错过了一个/\b[Aa]n?( \w+)? shame\b(*SKIP)(*FAIL)|\bshame\b/

还有一件事:你不能在程序中加上 DROP PROCEDURE IF EXISTS removeDuplicates; DELIMITER $$ CREATE PROCEDURE removeDuplicates(str TEXT , OUT OUT_STR_RESULT TEXT) BEGIN DECLARE temp_word TEXT; DECLARE last_word TEXT; DECLARE result TEXT; DECLARE finished INT DEFAULT false; DECLARE words_cursor CURSOR FOR SELECT word FROM explosion; DECLARE CONTINUE handler FOR NOT found SET finished = true; CALL explode(str); DROP TABLE IF EXISTS temp_words; CREATE TABLE temp_words (id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, t VARCHAR(100)); OPEN words_cursor; loop_words: LOOP FETCH words_cursor INTO temp_word; IF finished THEN LEAVE loop_words; IF temp_word = "" THEN SET result = CONCAT(result, temp_word, " "); SET last_word = temp_word; ELSEIF last_word = temp_word THEN SET last_word = temp_word; ELSE INSERT INTO temp_words (t) VALUES (temp_word); END IF; END IF; -- i had made changes at this line. END LOOP loop_words; CLOSE words_cursor; SET OUT_STR_RESULT =result; -- RETURN result; END$$ ; 陈述;它只能在END IF中使用。而不是那样,你可以使用RETURN参数。

尝试以上代码。