Java 与 oracle merge/upsert 的预准备语句 - 索引处缺少 IN 或 OUT 参数
Java's prepared statement with oracle merge/upsert - Missing IN or OUT Parameter at index
我正在尝试对单个 table 执行 MERGE(如果不存在则插入其他更新)。
我准备好的语句如下所示:
merge into table USING DUAL ON (primarykey=?)
when not matched then insert (colum1,colum2,columnprimarykey,column4,column5,column6,column7,column8,column9,column10,column11,column12) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
when matched then update set column1=?,column2=?,column4=?,column5=?,column6=?,column7=?,column8=?,column9=?,column10=?,column11=?,column12=?
但是在执行准备好的语句时出现以下错误:
Caused by: java.sql.SQLException: Missing IN or OUT parameter at index:: 13
at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1937) ~[ojdbc8-19.3.0.0.jar!/:19.3.0.0.0]
以下在 SQL 开发人员中直接执行时工作正常,如果在 table 中找不到 primarykeycolumn=99,它将插入一个包含 12 列的新记录。如果在 table.
中找到 primarykeycolumn=99,它将更新其余 11 列
merge into table USING DUAL ON (primarykey=99)
when not matched then insert
(colum1,colum2,column3/primarykey,column4,column5,column6,column7,column8,column9,column10,column11,column12) VALUES (somevalue,somevalue,99,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue)
when matched then update set
column1=somevalue,column2=somevalue,column4=somevalue,column5=somevalue,column6=somevalue,column7=somevalue,column8=somevalue,column9=somevalue,column10=somevalue,column11=somevalue,column12=somevalue
Java代码:
public Mono<String> createData(final Mono<Record> inputMono) {
final String someID = UUID.randomUUID().toString();
final Mono<Integer> asyncUpdate = inputMono.flatMap(record -> {
return beginUpdate(dataSource,
sqlStatementSharedAbove).withStatementEnricher(stmt -> {
stmt.setString(1, record.getFirstName());
stmt.setString(2, record.getLastName());
stmt.setInt(3, record.getUserId());
stmt.setString(4, record.getCOMPANYNAME());
stmt.setInt(5, record.getCOMPANYID());
stmt.setString(6, record.getEMAILID());
stmt.setTimestamp(7,Timestamp.valueOf(now));
stmt.setTimestamp(8,Timestamp.valueOf(now));
stmt.setString(9,record.getDEPARTMENTNAME());
stmt.setInt(10, record.getActiveInd());
stmt.setInt(11, record.getTypeCode());
stmt.setString(12, record..getTypeName());
}).build();
});
return asyncUpdate.doOnSuccess(affectedRows -> LOGGER.debug("Added
{} rows with ID {}", affectedRows, someID))
.map(affectedRows -> someID);
}
UPDATE 部分只能使用 USING 部分的 table(查询)返回的列值。
所以你需要将你的语句重写成这样:
merge into the_table USING
(
select ? as primarykey,
? as column1,
? as column2,
? as column3,
....
from dual
) x ON (x.primarykey = the_table.primarykey)
when not matched then
insert (column1,column2,primarykey,column3, ...)
VALUES (x.column1, x.column2, x.primarykey, x.column3, ...)
when matched then update
set column1 = x.column1,
column2 = x.column2,
....
我正在尝试对单个 table 执行 MERGE(如果不存在则插入其他更新)。
我准备好的语句如下所示:
merge into table USING DUAL ON (primarykey=?)
when not matched then insert (colum1,colum2,columnprimarykey,column4,column5,column6,column7,column8,column9,column10,column11,column12) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
when matched then update set column1=?,column2=?,column4=?,column5=?,column6=?,column7=?,column8=?,column9=?,column10=?,column11=?,column12=?
但是在执行准备好的语句时出现以下错误:
Caused by: java.sql.SQLException: Missing IN or OUT parameter at index:: 13 at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1937) ~[ojdbc8-19.3.0.0.jar!/:19.3.0.0.0]
以下在 SQL 开发人员中直接执行时工作正常,如果在 table 中找不到 primarykeycolumn=99,它将插入一个包含 12 列的新记录。如果在 table.
中找到 primarykeycolumn=99,它将更新其余 11 列merge into table USING DUAL ON (primarykey=99)
when not matched then insert
(colum1,colum2,column3/primarykey,column4,column5,column6,column7,column8,column9,column10,column11,column12) VALUES (somevalue,somevalue,99,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue,somevalue)
when matched then update set
column1=somevalue,column2=somevalue,column4=somevalue,column5=somevalue,column6=somevalue,column7=somevalue,column8=somevalue,column9=somevalue,column10=somevalue,column11=somevalue,column12=somevalue
Java代码:
public Mono<String> createData(final Mono<Record> inputMono) {
final String someID = UUID.randomUUID().toString();
final Mono<Integer> asyncUpdate = inputMono.flatMap(record -> {
return beginUpdate(dataSource,
sqlStatementSharedAbove).withStatementEnricher(stmt -> {
stmt.setString(1, record.getFirstName());
stmt.setString(2, record.getLastName());
stmt.setInt(3, record.getUserId());
stmt.setString(4, record.getCOMPANYNAME());
stmt.setInt(5, record.getCOMPANYID());
stmt.setString(6, record.getEMAILID());
stmt.setTimestamp(7,Timestamp.valueOf(now));
stmt.setTimestamp(8,Timestamp.valueOf(now));
stmt.setString(9,record.getDEPARTMENTNAME());
stmt.setInt(10, record.getActiveInd());
stmt.setInt(11, record.getTypeCode());
stmt.setString(12, record..getTypeName());
}).build();
});
return asyncUpdate.doOnSuccess(affectedRows -> LOGGER.debug("Added
{} rows with ID {}", affectedRows, someID))
.map(affectedRows -> someID);
}
UPDATE 部分只能使用 USING 部分的 table(查询)返回的列值。
所以你需要将你的语句重写成这样:
merge into the_table USING
(
select ? as primarykey,
? as column1,
? as column2,
? as column3,
....
from dual
) x ON (x.primarykey = the_table.primarykey)
when not matched then
insert (column1,column2,primarykey,column3, ...)
VALUES (x.column1, x.column2, x.primarykey, x.column3, ...)
when matched then update
set column1 = x.column1,
column2 = x.column2,
....