很多人都知道在Oracle中并没有像其他数据库中的自增字段,那么我们怎样才能实现Oracle的自增字段功能呢?下面我们通过Oracle中的Sequence和Trigger来实现此功能。1、首先建立一个创建自增字段的存储过程//Written by Sun Zhenfang 20040903 create or replace PRocedure pr_CreateIdentityColumn (tablename varchar2,columnname varchar2) as strsql varchar2(1000); begin strsql := 'create sequence seq_'tablename' minvalue 1 maxvalue 999999999999999999 start with 1 increment by 1 nocache'; execute immediate strsql; strsql := 'create or replace trigger trg_'tablename' before insert on 'tablename' for each row begin select seq_'tablename'.nextval into :new.'columnname' from dual; end;'; execute immediate strsql; end;2、Oracle中执行动态SQL时要显示授权(即使该用户拥有该相关权限)GRANT CREATE ANY SEQUENCE TO "UserName"; GRANT CREATE ANY TRIGGER TO "UserName";(注重:数据库用户名区分大小写)3、重新Compile存储过程pr_CreateIdentityColumn4、搞定,下面我们就可以用这个存储过程建立自增自段了。5、调用存储过程建立自增字段(Note: 第一个参数是表名,第二个参数为自增字段的名字)exec pr_createidentitycolumn('sdspdept','deptid'); exec pr_createidentitycolumn('sdspuser','userid'); exec pr_createidentitycolumn('sdspsysrole','sysroleid'); exec pr_createidentitycolumn('sdspfp','sysfpid'); exec pr_createidentitycolumn('sdspphasemodel','phasemodelid'); exec pr_createidentitycolumn('sdspphase','phaseid'); ... ...