SQL syntax error while trying to execute script with multiple CREATE VIEW statements via EntityManager

161 Views Asked by At

I would like to create a bunch of SQL views at my Quarkus application startup and I wrote

@Inject
EntityManager em;

public void exec(... sql) {
    logger.info("EXECUTING " ...);
    sql = "BEGIN\n" + sql + "\nEND";
    Query q = em.createNativeQuery(sql);
    q.executeUpdate();
}

And my SQL file is following:

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
...

Unfortunately, it throws exception, saying

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

If I copy paste the content of sql varible to database client and execute it, then it works, i.e. there is no actually syntax error. Probably it dislikes multiple queries or line ends or something.

What else can be suggested?

0

There are 0 best solutions below