批量修改Oracle序列值的預存程序

來源:互聯網
上載者:User

 使用Oracle序列來產生表主索引值的時候 ,可能會碰到會根據主索引值來修改相應序列的值。

    下面預存程序P_MODIFY_SEQUENCES完成這一目的。

  r:=F_MODIFY_SEQUENCE('SE_DATATYPE','T_DATATYPE','DDID');
     r:=F_MODIFY_SEQUENCE('SE_DATAITEM','T_DATAITEM','ITEMID');

把這個修改成要修改的序列名及對應表名和主鍵名,支援多序列。 create   or   replace   procedure  P_MODIFY_SEQUENCES  is
-- 更新各序列值
str_sql  varchar2 ( 4000 );
r boolean;

function  F_MODIFY_SEQUENCE(sequenceName  varchar2 ,taleName  varchar2 ,keyF  varchar2 ) return  boolean
is
  LastValue  integer ;
  f_sql  varchar2 ( 4000 );
  Next_num  number ;
  Max_num  number ;
begin
 f_sql: = ' select max( ' || keyF || ' ) from  ' || taleName;
  execute  immediate f_sql  into  Max_num;
  if (Max_num  is   not   null )  then
      Next_num: = Max_num + 1 ;
     f_sql: = ' alter sequence  ' || sequenceName || '  increment by 1 nocache ' ;
      execute  immediate f_sql;
      -- f_sql:='select '||sequenceName||'.nextval from dual';
      -- execute immediate f_sql;
     f_sql: = ' alter sequence  ' || sequenceName || '  increment by 1 nocache ' ;
      execute  immediate f_sql;
 
     loop
      f_sql: = ' select  ' || sequenceName || ' .nextval from dual ' ;
       execute  immediate f_sql  into  LastValue;
       -- select SE_DATATYPE.nextval into LastValue from dual;    
       exit   when  LastValue  >=  Next_num  -   1 ;
      f_sql: = ' select  ' || sequenceName || ' .nextval from dual ' ;
       execute  immediate f_sql  into  LastValue;    
       -- select SE_DATATYPE.nextval into LastValue from dual;
      end  loop;  
     f_sql: = ' alter sequence  ' || sequenceName || '  increment by 1 cache 20 ' ;
      execute  immediate f_sql;
  end   if ;
  commit ;
  return  true;
 Exception
     when  others  then
             return  false; 
end  F_MODIFY_SEQUENCE;


begin  
     r: = F_MODIFY_SEQUENCE( ' SE_DATATYPE ' , ' T_DATATYPE ' , ' DDID ' ); 
     r: = F_MODIFY_SEQUENCE( ' SE_DATAITEM ' , ' T_DATAITEM ' , ' ITEMID ' ); 
end  P_MODIFY_SEQUENCES;
/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.