Domanda

I was implementing upsert according to this great post: Insert, on duplicate update in PostgreSQL? and this works really fine with psql. However, I can't fire the same query into hsqldb which i use for testing. my sql looks like this:

UPDATE account set name = ?, email = ?, type = ?, regts = ?, dao_updated = ? WHERE id = ?; 
INSERT INTO account (id, name, email, type, regts, dao_created,dao_updated) SELECT ?,?,?,?,?,?,? WHERE NOT EXISTS (SELECT 1 FROM account WHERE id = ? );

I get the error message:

Caused by: java.sql.SQLSyntaxErrorException: unexpected token: INSERT
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)

Is this a limitation of hsqldb or am I doing something wrong?

thanks in advance Leon

È stato utile?

Soluzione

HSQLDB supports the MERGE statement for this:

http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_merge_statement

The merge statement is generally more powerful than UPSERT and non-standard alternatives.

Alternatively, you can use CREATE PROCEDURE and define a procedure similar to the PostgreSQL example given in the linked answer.

http://hsqldb.org/doc/2.0/guide/sqlroutines-chapt.html#src_psm_conditional

You can use the GET DIAGNOSTICS statement to find out if the update succeeded.

http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_diagnostics_state

Altri suggerimenti

You apparently can't use several statements inside single executeUpdate() call. Please, call twice first time doing UPDATE, next time doing INSERT

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top