2017-01-20 154 views
0

我有一個以下查詢,它需要通過使用列作爲鍵並返回生成的鍵來選擇一行。java.sql.SQLSyntaxErrorException:ORA-00933:SQL命令未正確結束以進行INSERT INTO SELECT

INSERT INTO t_tpms_cc_request 
      (process_identifier, 
      request_source_id, 
      amount, 
      etc_account_id, 
      retry_count, 
      status, 
      store_identifier, 
      version_no, 
      next_process_time, 
      composite_transaction_id, 
      payment_id, 
      processed_time, 
      replenishment_id, 
      pay_type, 
      agency_id, 
      response_code, 
      file_id, 
      request_date, 
      auth_file_id, 
      auth_date_time, 
      merc_file_id, 
      merc_date_time, 
      cc_num, 
      cc_expiration_date, 
      merchant_id, 
      ext_sys_ref, 
      encrypt_cc_number, 
      cc_month_cd, 
      cc_year_cd, 
      orig_txn_ref, 
      auth_code, 
      avs_code, 
      cvv_code) 
SELECT CC.process_identifier, 
     CC.request_source_id, 
     CC.amount, 
     CC.etc_account_id, 
     CC.retry_count, 
     CC.status, 
     CC.store_identifier, 
     CC.version_no, 
     CC.next_process_time, 
     CC.composite_transaction_id, 
     CC.payment_id, 
     CC.processed_time, 
     CC.replenishment_id, 
     CC.pay_type, 
     CC.agency_id, 
     CC.response_code, 
     CC.file_id, 
     CC.request_date, 
     CC.auth_file_id, 
     CC.auth_date_time, 
     CC.merc_file_id, 
     CC.merc_date_time, 
     CC.cc_num, 
     CC.cc_expiration_date, 
     CC.merchant_id, 
     CC.ext_sys_ref, 
     CC.encrypt_cc_number, 
     CC.cc_month_cd, 
     CC.cc_year_cd, 
     CC.orig_txn_ref, 
     CC.auth_code, 
     CC.avs_code, 
     CC.cvv_code 
FROM t_tpms_cc_request CC 
WHERE CC.order_id = ? 

而且,我已經wrriten一個下面的Java代碼來做到這一點:

String key[] = {"order_id"}; 

DataSource ds = null; 
Connection con = null; 
ResultSet rs = null; 
try { 
     ds = jdbcTemplate.getDataSource(); 
     con = ds.getConnection(); 
     PreparedStatement ps = 
      con.prepareStatement(insertCCRequest.trim(), key); 
    ps.setString(1, OrderId); 
     int i= ps.executeUpdate(); 
     rs = ps.getGeneratedKeys(); 
     if (rs.next()) { 
     return rs.getString(1); 
     } 
} catch (SQLException e) { 
    logger.debug("SQL exception in RebillDao.insertCCrequest() 
             method..!! "); 
    logger.debug("Exception cause: "+e.getMessage()); 
    e.printStackTrace(); 
    throw e; 
} 
finally { 
     if(con!=null){ 
      con.close(); 
     }   
} 
return ""; 

當我跑,我得到異常如下:

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended 

請告訴我的方式解決這個問題。

此外,使用JDK 1.6ojdbc6-11.2.0.4.jar

+0

請正確編輯您的代碼。 – surajsn

+0

你需要在你的SQL結尾處使用分號嗎? –

+0

您是否嘗試使用大寫字母名稱? 'String key [] = {「ORDER_ID」}; ' –

回答

0

我懷疑,當您使用一份聲明中生成的密鑰,Oracle JDBC驅動程序添加RETURNING INTO子句的INSERT聲明,並且JDBC驅動程序太暗以致於無法識別RETURNING INTO子句不能與INSERT INTO ... SELECT ...語句一起使用。如果我嘗試運行INSERT INTO ... SELECT ... RETURNING ...語句,我會得到相同的ORA-00933錯誤。

什麼你可以嘗試,而不是一個PL/SQL塊,我們獲取的「老」行插入一條記錄,然後使用INSERT ... VALUES語句和RETURNING_INTO子句中的值插入「新」行:

DECLARE 
    l_row  t_tpms_cc_request%ROWTYPE; 
BEGIN 
    SELECT * INTO l_row FROM t_tpms_cc_request WHERE order_id = ?; 
    INSERT INTO t_tpms_cc_request (some_column, some_other_column, ...) 
    VALUES (l_row.some_column, l_row.some_other_column, ...) 
    RETURNING order_id INTO ?; 
END; 

由於我們從中返回值,因此我們需要將其作爲CallableStatement而不是PreparedStatement進行準備,並且需要將參數2註冊爲out參數。然後,我們可以使用此輸出參數,而不是您目前使用的方法來返回生成的鍵值。

很明顯,這種方法是針對Oracle的,不適用於其他數據庫。我不知道問題數據庫可移植性對您有多大影響,也不知道您是否可以從其他數據庫中的INSERT INTO ... SELECT ...語句返回生成的密鑰。

相關問題