¡¡¡¡ºÜ¶àÈ˶¼ÖªµÀÔÚ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_CreateIdentityColumn
¡¡¡¡4¡¢¸ã¶¨£¬ÏÂÃæÎÒÃǾͿÉÒÔÓÃÕâ¸ö´æ´¢¹ý³Ì½¨Á¢×ÔÔö×Ô¶ÎÁË¡£
¡¡¡¡5¡¢µ÷Óô洢¹ý³Ì½¨Á¢×ÔÔö×ֶΣ¨Note: µÚÒ»¸ö²ÎÊýÊDZíÃû£¬µÚ¶þ¸ö²ÎÊýΪ×ÔÔö×ֶεÄÃû×Ö£©
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');
... ...