带Liquibase sqlFile的MySQLSyntaxErrorException

时间:2019-06-11 11:41:27

标签: mysql liquibase

当我运行'sqlFile'类型的Liquibase changeSet来插入数据时,我得到一个MySQLSyntaxErrorException。我的查询是正确的,因为它可以在MySQL工作台中正常运行。

这是我的Liquibase changeSet:

INSERT INTO table1 (table1col1, table1col2)
VALUES ('table1col1value', 'table1col2value')
;

-- link to the latest value of table1
INSERT INTO table2 (table2col1, table2col2)
VALUES ('table2col1value',
(SELECT table1.id FROM table1 ORDER BY table1.id DESC LIMIT 1))
;

这是SQL查询:

    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:301)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:107)
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1251)
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1234)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:554)
    ... 53 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO table2 (table2col1, table2col2)
VALUES (' at line 5
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2483)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2441)
    at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:845)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:745)
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:299)
    ... 58 common frames omitted

这是错误:

save_vars <- function(list.of.vars = NULL,
                      prefix = "StatusQuo",
                      path = "data") {
  if(is.null(list.of.vars)) list.of.vars <- ls()
  date_time <- Sys.time()
  if (!is.null(path))
    path <- paste0(path, "/")
  file_name <- paste0(path, prefix, "_", date_time, ".RData")
  save(list = list.of.vars, file = file_name)
}

您知道Liquibase是否能够处理这样的嵌套查询吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试以这种方式选择插入内容

INSERT INTO table2 (col1, col2)
select  'table2col1value' , id 
FROM table1 
ORDER BY table1.id DESC 
LIMIT 1

答案 1 :(得分:0)

我最终使用<changeSet><sql>而不是<changeSet><sqlFile>进行完全相同的查询,并且有效:

    <changeSet id="1" author="me">
        <sql> 
INSERT INTO table1(table1col1, table1col2)
VALUES (table1col1value, table1col2value)
;
        </sql>
        <sql>
INSERT INTO table2 (table2col1, table2col2)
VALUES (table2col1value,
(SELECT table1.id FROM table1 ORDER BY table1.id DESC LIMIT 1))
;
        </sql>
    </changeSet>