2016-11-27 49 views
0

我想使用hana db獲取插入的自動增量ID。 我們嘗試過在互聯網上搜索也嘗試使用jdbc keyholder選項。 支持自動增加最近來到hana,我沒有得到相同的例子 有沒有辦法做到這一點?執行插入查詢後生成autoincrement id HANA

回答

2

我認爲「autoincrement」是指HANA SPS 8附帶的IDENTITY列選項?

CREATE COLUMN TABLE <table_name> (<column_name> <num_data_type 
    GENERATED BY DEFAULT AS IDENTITY); 

本博客文章解釋如何工作的:
https://blogs.sap.com/2014/06/04/quick-note-on-identity-column-in-sap-hana/

--- 報價: ---

這背後的魔力是序列創建的課程中:

select column_name, column_id from table_columns where table_name = 'SOME_NAMES' 

+-------------+-----------+ 
| COLUMN_NAME | COLUMN_ID | 
+-------------+-----------+ 
| ID   | 145210 | 
| NAME  | 145211 | 
+-------------+-----------+ 

select * from sequences where sequence_name like ‘%145210%’ 

+-------------+---------------------------+--------------+--------------+-----------+------------------+--------------+-----------+-------------------------------------------+------------+ 
| SCHEMA_NAME | SEQUENCE_NAME   | SEQUENCE_OID | START_NUMBER | MIN_VALUE | MAX_VALUE  | INCREMENT_BY | IS_CYCLED | RESET_BY_QUERY       | CACHE_SIZE | 
+-------------+---------------------------+--------------+--------------+-----------+------------------+--------------+-----------+-------------------------------------------+------------+ 
| SYSTEM  | _SYS_SEQUENCE_145210_#0_# | 145215 |  1  | 1  |  4611686018 |    |   |           |   | 
+-------------+---------------------------+--------------+--------------+-----------+------------------+--------------+-----------+-------------------------------------------+------------+ 

Aaaaand,就是這樣。

--- 最終報價 ---

從這裏你可以使用CURRVAL得到最後產生的價值。

SELECT SYSTEM."_SYS_SEQUENCE_145210_#0_#".CURRVAL FROM DUMMY 
+0

thanks..does有辦法讓插入查詢響應 – Vish

+1

@Vish數據:不,你需要發送的SELECT作爲第二查詢之後,最好是在一個事務。或者,您可以編寫一個存儲過程,執行插入並返回新的自動增量ID。 – Hannobo