2011-08-27 98 views
1

在Postgres裏,我可以寫的Sybase JDBC獲取生成的密鑰

INSERT .. RETURNING * 

要檢索的插入過程中已經生成的所有值。在Oracle中,我可以使用HSQLDB

String[] columnNames = ... 
PreparedStatement stmt = connection.prepareStatement(sql, columnNames); 
// ... 
stmt.execute(); 
stmt.getGeneratedKeys(); 

檢索所有已生成的值。 MySQL是有限的,只返回設置爲AUTO_INCREMENT的列。但是,如何使用Sybase SQL Anywhere完成這項工作? JDBC驅動程序不實現這些方法,並且沒有INSERT .. RETURNING子句,如在Postgres中。有辦法做到這一點,除了可能運行

SELECT @@identity 

插入後立即?

回答

1

我當前實現執行三個連續的SQL語句:

-- insert the data first 
INSERT INTO .. VALUES (..) 

-- get the generated identity value immediately afterwards 
SELECT @@identity 

-- get the remaining values from the record (possibly generated by a trigger) 
SELECT * FROM .. WHERE ID = :previous_identity 

第三條語句可以省略,如果要求只有ID