我有以下代码运行针对SQL Server数据库的查询。我已经阅读了这些链接
http://jenikya.com/blog/2009/02/sqlexception-select-top-number.html
MS SQL Exception: Incorrect syntax near '@P0'
但我仍然无法看到我在哪里/为什么会收到'@ P0'问题。我把TOP参数包装在括号中。
private String DEP_AMC_QUERY = "SELECT TOP(1) ((fund_amc-reinsurance_premium)/1000)
as dep_amc“+ “FROM Hedging_Staging.dbo.:table”+ “WHERE internal_fund_code_identifier =:fund”+ “AND load_id =:load_id;”;
public BigDecimal getDepAmcValue(String fund,Long load_id,String table){
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(getDataSource());
//Set up parameters
MapSqlParameterSource namedParameters = new MapSqlParameterSource("fund",fund);
namedParameters.addValue("load_id",load_id);
namedParameters.addValue("table",table);
MapUtils.debugPrint(System.out,"params", namedParameters.getValues());
//Execute query
return jdbcTemplate.queryForObject(DEP_AMC_QUERY,namedParameters,BigDecimal.class);
}
控制台和异常消息是
13:11:12,871 INFO [ReinsuredFundAssetProcessor] looking up dep_amc value for AXX in AI_IFL_Policy table.
params =
{
table = AI_IFL_Policy java.lang.String
fund = AXX java.lang.String
load_id = 4356 java.lang.Long
}
13:11:12,909 ERROR [AbstractStep] Encountered an error executing the step
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select top(1) ((fund_amc-reinsurance_premium)/1000) as dep_amc from Hedging_Staging.dbo.? WHERE internal_fund_code_identifier=? AND load_id=?;]; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
有什么想法吗?