我想在我的 Quarkus 应用程序启动时创建一堆 SQL 视图,我写了
@Inject
EntityManager em;
public void exec(... sql) {
logger.info("EXECUTING " ...);
sql = "BEGIN\n" + sql + "\nEND";
Query q = em.createNativeQuery(sql);
q.executeUpdate();
}
我的 SQL 文件如下:
CREATE OR REPLACE VIEW My_view_ex AS
SELECT cc.*, If(cc.beautifulName IS NULL, cc.name, cc.beautifulName) canonName FROM My_table cc;
CREATE OR REPLACE VIEW MyView2 AS
...
不幸的是,它抛出异常,说
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE OR REPLACE VIEW My_view_ex AS
SELECT cc.*, If(cc.beautifulName IS NU...' at line 2
如果我将变量的内容复制粘贴sql
到数据库客户端并执行它,那么它可以工作,即实际上没有语法错误。可能它不喜欢多个查询或行尾之类的。
还有什么可以建议的?