2013-01-24 34 views
6

我有嘗試參數間隔參數在我的Oracle查詢的問題:Spring命名參數:如何在我的查詢中參數化Oracle間隔?

select current_timestamp - interval :hours hour from dual 

如果我更換固定的時間間隔參數,然後執行就好了。

在SQL中嘗試了帶引號和不帶引號的參數。

使用以下最小片段見的圖示:

public class Main { 
    private static String SQL_CONSTANT_INTERVAL = "select current_timestamp - interval '1' hour from dual"; 

    private static String SQL_PARAMETERIZED_INTERVAL_QUOTED = "select current_timestamp - interval ':hours' hour from dual"; 

    private static String SQL_PARAMETERIZED_INTERVAL_UNQUOTED = "select current_timestamp - interval :hours hour from dual"; 

    public static void main(String[] args) throws Exception { 
     Properties properties = new Properties(); 
     properties.load(Main.class.getClassLoader().getSystemResourceAsStream("db.properties")); 
     DataSource dataSource = BasicDataSourceFactory.createDataSource(properties); 

     NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(dataSource); 

     Map<String, String> params = Collections.singletonMap("hours", "1"); 

     String[] queries = 
       new String[] { SQL_CONSTANT_INTERVAL, SQL_PARAMETERIZED_INTERVAL_QUOTED, SQL_PARAMETERIZED_INTERVAL_UNQUOTED }; 

     for (String q : queries) { 
      System.out.println("Executing " + q); 
      try { 
       System.out.println("Result = " + npTemplate.queryForObject(q, params, String.class)); 
      } catch (RuntimeException e) { 
       System.out.println("Error: " + e); 
      } 
      System.out.println(); 
     } 
    } 
} 

輸出:

Executing select current_timestamp - interval '1' hour from dual 
Result = 2013-01-24 18:55:16.373 Europe/Moscow 

Executing select current_timestamp - interval ':hours' hour from dual 
24-Jan-2013 20:55:16 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml] 
24-Jan-2013 20:55:16 org.springframework.jdbc.support.SQLErrorCodesFactory <init> 
INFO: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase] 
Error: org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [select current_timestamp - interval ':hours' hour from dual]; ORA-01867: the interval is invalid 
; nested exception is java.sql.SQLDataException: ORA-01867: the interval is invalid 


Executing select current_timestamp - interval :hours hour from dual 
Error: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select current_timestamp - interval ? hour from dual]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected 
+0

的源代碼:https://gist.github.com/4632533 – vitaly

回答

7

interval '1' hour是Oracle literal。因此,您無法使用綁定參數替換中間的'1'

相反,你可以這樣做:

NUMTODSINTERVAL(1, 'HOUR') 

然後你就可以更換整數文字在Java SQL就像這樣:

NUMTODSINTERVAL(?, 'HOUR') 
+0

像魅力一樣工作,謝謝。我沒有意識到Spring(或JDBC?)在替代文字變量時存在問題 – vitaly

+2

實際上,這是Oracle的限制。你的Spring/JDBC查詢變量在數據庫執行的實際查詢中變成Oracle綁定變量,並且你根本無法使用綁定變量來替換Oracle文本中間的東西。 – GriffeyDog