2011-08-27 80 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