简体   繁体   中英

Regenerate all sequences in db (oracle)

Good day. I have a very specific task: regenerate all sequences in database. There is a 400+ tables in it, so I can't do it by hands. Can somebody help me to do it? Thanks a lot..

Please note this is highly dangerous. You may very well make mistakes. Run the select first to check what you're about to do and create a table of the select so you can re-create the synonyms manually later if you need to.

Using all_synonyms or dba_synonyms instead of user_synonyms may result in dropping system synonyms do not do this if you want your database to work afterwards

I'd also recommend testing the code on 1 test synonym you create to ensure that it does exactly what you want.

Plus I don't really see the point of doing this at all? If the synonyms are there why do you need to re-generate them? Or is this being done on another server? If so add @server_name after user_synonyms and remove the drop .

begin

  for xx in ( select * from user_sequences ) loop

    execute immediate 'drop sequence ' || xx.sequence_name;
    execute immediate 'create sequence ' || xx.sequence_name
                      ||  ' start with ' || xx.min_value
                      ||  '  ends with ' || xx.max_value
                      || case when xx.cycle_flag = 'N' then ' nocycle ' 
                              else ' cycle ' end
                      || case when xx.cache_size = 0 then ' nocache ' 
                              else ' cache ' end || xx.cache_size
                      || case when xx.order_flag = 'N' then ' noorder ' 
                              else ' order ' end
                      ;

   end loop;

end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM