문제

In Postgres, I can write

INSERT .. RETURNING *

To retrieve all values that had been generated during the insert. In Oracle, HSQLDB, I can use

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

To retrieve all values that had been generated. MySQL is a bit limited and only returns columns that are set to AUTO_INCREMENT. But how can this be done with Sybase SQL Anywhere? The JDBC driver does not implement these methods, and there is no INSERT .. RETURNING clause, as in Postgres. Is there way to do it, other than maybe running

SELECT @@identity

immediately after the insert?

도움이 되었습니까?

해결책

My current implementation executes three consecutive SQL statements:

-- 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

The third statement can be omitted, if only the ID column is requested

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top