Oracel--实现自动编号字段
转自http://yxl864.blog.163.com/blog/static/140608558201023001239317/
SQL Server,Sybase:
有個identity屬性可以讓系統自動增1
create table a
(
a1 int identity(1,1),
a2 varchar(6)
)
然後在insert時:
insert into a values('hello!'); --不用管a1,系統會幫你自動增1
Oracle:
使用SEQUENCE(序列)可以達到你的要求
create table a
(
a1 int ,
a2 varchar2(6)
);
create SEQUENCE seq_a INCREMENT BY 1;
然後在insert時:
insert into a values(seq_a.nextval,'hello!'); --seq_a.nextval是該序列的下個值
代码:
decline_id_sql = "select to_char(S_DECLINE_ID.nextval) DECLINE_ID from DUAL";
try {
dba = new DBAccess(con);
ResultSet rs = dba.openSelect(decline_id_sql);
rs.beforeFirst();
if (rs.next()) {
decline_id = StringUtil.escapeSQLTags((rs
.getString("DECLINE_ID")));
}
declineItemBean.setDecline_id(decline_id);
rs.close();
dba.closeSelect();
} catch (Exception ex) {
}