在 SQL 语法中连接变量
Concatenating variable in SQL syntax
我有以下内容:
public void getProceduredName(long fileId, String proceduredName ){
final String sql = "call " + proceduredName + " (:fileId)";
SqlParameterSource namedParameters = new MapSqlParameterSource("fileId", fileId);
try {
namedParameterJdbcTemplate.update(sql, namedParameters);
} catch (Exception e){
...
}
}
所以一旦它读取该行并尝试在 namedParameterJdbcTemplate 上使用它,它会说:bad SQL grammar [call procedureName (?)]; nested exception is java.sql.SQLException: ORA-06576: not a valid function or procedure name
现在...如果我这样说:
final String sql = "call hardCodedName (12345)";
然后就可以了...
我该如何解决才能传入变量和参数?
我看了这里:Spring JDBC Template for calling Stored Procedures 并没有解决问题。
什么有效:
final String sql = "call proceduredName (:fileId)";
我正在尝试做的事情:
final String sql = "call " + proceduredName + " (:fileId)";
我觉得当我在tring中间传入变量时,它不再将字符串作为sql语法,所以它只作为语法:"call "
和" (:fileId)"
如何让它与变量连接一起工作?
谢谢
编辑:
我这样调用 getProceduredName :
Dao.getProceduredName(fileId, proceduredName);
其中 dao 是接口,daoImp 将具有 getProceduredName() 的实际实现。我传递的只是 ID 类型 Long,以及过程名称类型 String
我这个问题的Syntax其实是对的,我在procedureName前面漏了DB的名字,所以应该是这样的:
final String sql = "call sqlName." + proceduredName + " (:fileId)";
我有以下内容:
public void getProceduredName(long fileId, String proceduredName ){
final String sql = "call " + proceduredName + " (:fileId)";
SqlParameterSource namedParameters = new MapSqlParameterSource("fileId", fileId);
try {
namedParameterJdbcTemplate.update(sql, namedParameters);
} catch (Exception e){
...
}
}
所以一旦它读取该行并尝试在 namedParameterJdbcTemplate 上使用它,它会说:bad SQL grammar [call procedureName (?)]; nested exception is java.sql.SQLException: ORA-06576: not a valid function or procedure name
现在...如果我这样说:
final String sql = "call hardCodedName (12345)";
然后就可以了...
我该如何解决才能传入变量和参数?
我看了这里:Spring JDBC Template for calling Stored Procedures 并没有解决问题。
什么有效:
final String sql = "call proceduredName (:fileId)";
我正在尝试做的事情:
final String sql = "call " + proceduredName + " (:fileId)";
我觉得当我在tring中间传入变量时,它不再将字符串作为sql语法,所以它只作为语法:"call "
和" (:fileId)"
如何让它与变量连接一起工作?
谢谢
编辑:
我这样调用 getProceduredName :
Dao.getProceduredName(fileId, proceduredName);
其中 dao 是接口,daoImp 将具有 getProceduredName() 的实际实现。我传递的只是 ID 类型 Long,以及过程名称类型 String
我这个问题的Syntax其实是对的,我在procedureName前面漏了DB的名字,所以应该是这样的:
final String sql = "call sqlName." + proceduredName + " (:fileId)";