I'm trying to write some functional tests for our java application using H2 in-memory database. Since we use Oracle as our production DB, some of the code is written in PL/SQL. So I switched on Oracle compatibility mode and now my url looks like this: jdbc:h2:mem:test;MODE=Oracle
.
The actual application code that I'm having troubles with looks like this (note, both sequence and table have been created before the call):
CallableStatement = con.prepareCall(
" begin insert into profiles(" + PROFILE_FIELDS + ") " +
" values (sq_profiles.nextval, ?, ?, ?, ?, ?) " +
" returning profile_id into ?; end;");
It fails with the same error that this sample code I wrote to test begin ... end;
behavior:
Connection con = connectionProvider.getConnection()
con.prepareStatement("CREATE TABLE test (id INT NOT NULL)").execute()
con.prepareStatement("INSERT INTO test SELECT 1 FROM dual").execute()
con.prepareStatement("BEGIN INSERT INTO test SELECT 2 FROM dual; END;").execute()
The error I get:
Syntax error in SQL statement "BEGIN INSERT[*] INTO TEST SELECT 2 FROM DUAL; END; "; SQL statement:
BEGIN INSERT INTO test SELECT 2 FROM dual; END; [42000-167]
So my question is: are begin ... end;
blocks supported by H2? If yes, what could be wrong with the code above?
Thanks.
No, H2 Oracle compatibility mode does not mean support for such a PL/SQL. As told in H2 documentation, Oracle compatibility mode brings following:
- For aliased columns, ResultSetMetaData.getColumnName() returns the alias name and getTableName() returns null.
- When using unique indexes, multiple rows with NULL in all columns are allowed, however it is not allowed to have multiple rows with the same values otherwise.
- Concatenating NULL with another value results in the other value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With