【问题标题】:Sybase JDBC get generated keysSybase JDBC 获取生成的密钥
【发布时间】:2011-08-27 12:08:20
【问题描述】:

在 Postgres 中,我可以写

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

插入后立即?

【问题讨论】:

    标签: insert sqlanywhere identity-column lastinsertid


    【解决方案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 列,则可以省略第三条语句

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2013-03-19
      • 2018-11-06
      • 2015-04-02
      • 1970-01-01
      • 2012-02-01
      • 2015-11-20
      相关资源
      最近更新 更多