I have an issue with an INSERT
query which I have boiled down to the following problem.
My application uses Jdbi (version 3) and micronaut to connect to an Oracle 12c database. The query is performed as follows:
jdbi.withHandle<ProjectId, StatementException> {
val projectId = it.createUpdate("""
INSERT INTO projects(
"number",
site,
candidate,
creator_id,
creator_name,
editor_id,
editor_name,
parent_id,
status,
last_status_change
)
SELECT
107,
'SITE',
'CAND',
'a',
'a',
'a',
'a',
null,
'NEW',
'02-FEB-22 09.00.00 AM EUROPE/ROME'
FROM DUAL
""".trimIndent())
.executeAndReturnGeneratedKeys("id")
.mapTo(ProjectId::class.java)
.one()
}
This results in the following error:
java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
[statement:"INSERT INTO projects(
"number",
site,
candidate,
creator_id,
creator_name,
editor_id,
editor_name,
parent_id,
status,
last_status_change
)
SELECT
107,
'SITE',
'CAND',
'a',
'a',
'a',
'a',
null,
'NEW',
'02-FEB-22 09.00.00 AM EUROPE/ROME'
FROM DUAL", arguments:{positional:{}, named:{}, finder:[]}]
If I copy & paste the same query on a sqlplus console it works fine:
SQL> INSERT INTO projects(
2 "number",
3 site,
4 candidate,
5 creator_id,
6 creator_name,
7 editor_id,
8 editor_name,
9 parent_id,
10 status,
11 last_status_change
12 )
13 SELECT
14 119, -- changed number to avoid unique constraint issues
15 'SITE',
16 'CAND',
17 'a',
18 'a',
19 'a',
20 'a',
21 null,
22 'NEW',
23 '02-FEB-22 09.00.00 AM EUROPE/ROME'
24 FROM DUAL
25 ;
1 row created.
How can I make the query work with Jdbi?
NOTE: this is a dumbed down version of the query, in my real query I cannot use VALUES
in place of the SELECT ... FROM DUAL
because I have to compute the "number"
, I simplified the query and endedup with reproducing the same problem with the above simple query.
The real query is more like:
INSERT INTO projects(
"number",
site,
candidate,
creator_id,
creator_name,
editor_id,
editor_name,
parent_id,
status,
last_status_change
)
SELECT
(
SELECT MAX("number") FROM (
SELECT MAX("number")+1 as "number" FROM projects WHERE site = :site_1
UNION
SELECT 0 FROM DUAL WHERE NOT EXISTS (SELECT * FROM projects WHERE site = :site_2)
)
),
:site,
:candidate,
:creator_id,
:creator_name,
:editor_id,
:editor_name,
:parent_id,
:status,
:last_status_change
FROM DUAL