事件 sql 脚本中的 MySQL 语法错误。分隔符已被使用

Error with MySQL syntax in event sql-script. Delimeter already used

我有这个脚本每月 table 更新:

DELIMITER |
CREATE
    EVENT `kpiparams_scheduled_update`
    ON SCHEDULE
        EVERY 1 MONTH
            STARTS '2020-02-01 02:59:59'
    ON COMPLETION PRESERVE
    COMMENT 'KPIParams was updated by event_kpiparams_scheduled_update'
    DO
    BEGIN
        UPDATE kpiparams INNER JOIN kpiparams_update
            ON kpiparams.param_name = kpiparams_update.param_name
        SET kpiparams.good        = kpiparams_update.good,
            kpiparams.bad         = kpiparams_update.bad,
            kpiparams.weight_gold = kpiparams_update.weight_gold,
            kpiparams.weight_tech = kpiparams_update.weight_tech,
            kpiparams.is_for_calc = kpiparams_update.is_for_calc
        WHERE kpiparams.param_name = kpiparams_update.param_name;
    END |
DELIMITER ;

此代码丢弃异常:

Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER | CREATE EVENT kpiparams_scheduled_update ON SCHEDULE EVERY 1 MONTH ST' at line 1

还有这个:

nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER | CREATE EVENT kpiparams_scheduled_update ON SCHEDULE EVERY 1 MONTH ' at line 1

我搜索了一些资源说,我需要使用 Delimeter,不要在 '' 中写月数并使用 BEGIN-END。顺便说一下,它对我没有帮助。

spring.datasource.schema=classpath*:mysql-scripts/event_update_kpiparams.sql

已编辑脚本:

CREATE
    EVENT kpiparams_scheduled_update 
        ON SCHEDULE EVERY 1 MONTH STARTS '2020-02-01 02:59:59'
    ON COMPLETION PRESERVE
    COMMENT 'KPIParams was updated by event_kpiparams_scheduled_update'
    DO
    BEGIN
        UPDATE kpiparams INNER JOIN kpiparams_update
            ON kpiparams.param_name = kpiparams_update.param_name
        SET kpiparams.good        = kpiparams_update.good,
            kpiparams.bad         = kpiparams_update.bad,
            kpiparams.weight_gold = kpiparams_update.weight_gold,
            kpiparams.weight_tech = kpiparams_update.weight_tech,
            kpiparams.is_for_calc = kpiparams_update.is_for_calc
        WHERE kpiparams.param_name = kpiparams_update.param_name;
    END

我猜你试过 运行 使用 JDBC?

您根本不需要 DELIMITER |。那是 mysql client builtin command。 SQL 解析器无法识别客户端内置函数。

您可以将 CREATE EVENT 语句作为单个语句执行,然后您不需要在语句末尾使用分隔符。分隔符仅在支持多语句的接口中很重要(例如 mysql 客户端)。


好吧,您似乎在 .sql 文件中使用了多个语句,您需要某种方式来分隔这些语句。通常这是 ; 但您有一些语句包含 ; 作为语句的一部分,而不是作为分隔符。

我不是 Spring 开发人员,但我发现 描述了以下用法:

spring.datasource.separator

这也记录在案:https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

spring.datasource.separator

(default) ;

Statement separator in SQL initialization scripts.