我想获取表中最后一个id插入的值。我怎么能这样做?
答案 0 :(得分:26)
我使用的解决方案是:
select id from NEW TABLE (insert into (val1, val2, ...) values ('lorem', 'ipsum', ...))
这将从DB中插入的最后一行获取id列:)
答案 1 :(得分:15)
SELECT IDENTITY_VAL_LOCAL() AS VAL FROM SYSIBM.SYSDUMMY1
请参阅docs。
答案 2 :(得分:0)
看看这个答案。
http://www.sitepoint.com/php-database-db2/
// get the last inserted ID into the specified table
// int lastInsertID(string $tblName)
function lastInsertID($tblName)
{
if ($this->transIsOpen())
{
$sql = "SELECT SYSIBM.IDENTITY_VAL_LOCAL() AS id FROM " . $tblName;
$rs = $this->query($sql);
return $this->fetch($rs, "id");
}
return -1;
}
或者这个
http://www.php.net/manual/en/function.db2-last-insert-id.php#98361
答案 3 :(得分:-3)
int keyId = -1;
preparedStatement.executeUpdate();
resultSet = preparedStatement.getGeneratedKeys();
if (resultSet.next()) {
keyId = rs.getInt(1);
}
https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getGeneratedKeys()
更新:并且不要忘记使用以下标志Statement.RETURN_GENERATED_KEYS
创建preparedStatement,否则它将无法工作)))