【问题标题】:get last inserted row id in ebean with sqlupdate使用 sqlupdate 在 ebean 中获取最后插入的行 ID
【发布时间】:2016-11-17 14:00:02
【问题描述】:

如果我使用 ebean sqlupdate 在 Java 中插入 Oracle

    Transaction tx = Ebean.beginTransaction();
    try {

Transaction tx = Ebean.beginTransaction();
try {

    String sqlString = "INSERT INTO customers VALUES (1001,'Nichols', 'Alexandra', '17 Maple Drive', "+ "'Nashua', 'NH','03062', SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE (-71.48923,42.72347,NULL), NULL, NULL))";
    SqlUpdate query = Ebean.createSqlUpdate(sqlString);
    query.execute();
    String sqlQuery =
            "SELECT @@IDENTITY AS 'Identity'";
    SqlQuery query2 = Ebean.createSqlQuery(sqlQuery);

    List<SqlRow> list = query2.findList();
    System.out.println(list.get(0));
} finally {
  tx.commit();
}

报错

[PersistenceException: Query threw SQLException:ORA-00936: missing expression
  Query was:
SELECT @@IDENTITY AS 'Identity'

]

如何获取最后插入行的 id?

【问题讨论】:

  • 在选择查询中使用 desc 顺序
  • @XING 仅当主键是自动递增的整数时才有效。

标签: sql oracle ebean


【解决方案1】:

在 INSERT、SELECT INTO 或大容量复制语句完成后,@@IDENTITY 包含该语句生成的最后一个标识值。

但这是在 sql 中所以我认为你应该创建 USP,你可以从 USP 返回最后插入的记录 id 作为 outparm

   String sqlString = "INSERT INTO customers VALUES (1001,'Nichols', 'Alexandra', '17 Maple Drive', "+ "'Nashua', 'NH','03062', SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE (-71.48923,42.72347,NULL), NULL, NULL))";
   SqlUpdate query = Ebean.createSqlUpdate(sqlString);
   query.execute();
   SELECT @@IDENTITY AS 'Identity';  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2019-11-21
    • 1970-01-01
    • 2023-02-17
    • 2019-10-28
    • 1970-01-01
    相关资源
    最近更新 更多