【发布时间】:2019-06-11 21:12:13
【问题描述】:
当我运行“sqlFile”类型的 Liquibase changeSet 来插入数据时,我得到了 MySQLSyntaxErrorException。我的查询是正确的,因为它可以在 MySQL 工作台中正常运行。
这是我的 Liquibase 变更集:
<changeSet id="1" author="me" >
<sqlFile dbms="mysql"
path="insert_my_data.sql"
relativeToChangelogFile="true"
splitStatements="true"
stripComments="true"
endDelimiter="EOF" />
</changeSet>
这里是sql查询:
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))
;
这是错误:
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
你知道 Liquibase 是否能够处理这样的嵌套查询吗?
【问题讨论】:
-
应该是
VALUES而不是VALUE对吧? -
我都试过了,结果一样
-
我认为任何单独的查询都没有问题。 MySQL 异常从头开始引用第二个插入查询,表明在插入关键字处发生语法错误。因此我相信 liquibase 可能无法一次执行多个 sql 查询。根据错误消息,它似乎使用 jdbc 连接到 MySQL。以下 SO 问题解答了如何配置 jdbc 以一次性执行多个 sql 语句:stackoverflow.com/questions/10797794/…
-
我在我的 JDBC 选项中添加了 allowMultiQueries=true,但我仍然遇到同样的错误。我想我会简单地使用像几个 '
这样的 Liquibase 语法'