views:

306

answers:

3

I have a number of tables that use the trigger/sequence column to simulate auto_increment on their primary keys which has worked great for some time.

In order to speed the time necessary to perform regression testing against software that uses the db, I create control files using some sample data, and added running of these to the build process.

This change is causing most of the tests to crash though as the testing process installs the schema from scratch, and the sequences are returning values that already exist in the tables. Is there any way to programtically say "Update sequences to max value in column" or do I need to write out a whole script by hand that updates all these sequences, or can I/should I change the trigger that substitutes the null value for the sequence to some how check this (though I think this might cause the mutating table problem)?

A: 

As part of your schema rebuild, why not drop and recreate the sequence?

Wayne
The schema rebuild drops every database object including the sequences. The problem is that when they are recreated they all have a default starting value of zero and and there's already records in the tables with id's of 0 through X from the sample data that is loaded with sqlldr.
Mark Roddy
+1  A: 

You can generate a script to create the sequences with the start values you need (based on their existing values)....

SELECT 'CREATE SEQUENCE '||sequence_name||' START WITH '||last_number||';' FROM ALL_SEQUENCES WHERE OWNER = your_schema

(If I understand the question correctly)

cagcowboy
I have a script that dumps the data into control files. I extended this process to also dump drop and create statements to a separate script that I run after importing all the data from the control files.
Mark Roddy
+1  A: 

Here's a simple way to update a sequence value - in this case setting the sequence to 1000 if it is currently 50:

alter sequence MYSEQUENCE increment by 950 nocache;
select MYSEQUENCE_S.nextval from dual;
alter sequence MYSEQUENCE increment by 1;

Kudos to the creators of PL/SQL Developer for including this technique in their tool.

JoshL