查询刚插入记录ID

查询刚插入记录ID
mysql:   select LAST_INSERT_ID() as lastid
mssqlsever:  
public int updatePara(String sql) {
  int id = 0;
  try {
   st = con.createStatement();
   if (st.executeUpdate(sql) == 1) {
    rs = st.executeQuery("SELECT @@IDENTITY AS IDX_NO");
    if (rs.next()) {
     id = rs.getInt(1);
    }
    con.commit();
   } else {
    con.rollback();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return id;
 }

oracle:
$query="select seq_atable.currval from dual";
seq_atable.currval 的值只有在同一次会话中,发生seq_atable.nextval后有效:) 所以不会存在取错值的问题。

oracle创建触发器,不用管ID
使用序列+触发器实现自增,插入语句不需要管自增字段
如:create or replace trigger trg_atable before insert on atable for each row begin select seq_atable.nextval into :new.id from dual; end;

插入数据:
create or replace trigger TRG_SEO_ONLINE_PHOTO
  before insert on SEO_ONLINE_PHOTO
  for each row
begin
  select SEQ_ONLINE_PHOTO.nextval into :new.ONPHOTO_ID from dual;
end;

你可能感兴趣的:(查询刚插入记录ID)