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_CreateIdentityColumn
4、搞定,下面我们就可以用这个存储过程建立自增自段了。
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');