tags:

views:

13

answers:

1

In Oracle we can do it like this:

declare current_max_value NUMBER;
begin select last_number+1 into current_max_value from USER_SEQUENCES where sequence_name = 'HIBERNATE_SEQUENCE';
execute immediate 'CREATE SEQUENCE SEQ__NEW_SEQUENCE MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH '||current_max_value|| ' CACHE 20 NOORDER NOCYCLE';

Is there a DB2 equivalent?

+1  A: 

DB2 has vaugely equivalent functionality.

If you just need to generate uninque keys then:-

CREATE TABLE MYTABLE (
     GENERATED_KEY                    BIGINT
        GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1, CACHE 100),
     MY_DATA                          VARCHAR(1000) ........... 

On the create table statement will accomplish this without too much fuss. Anytime a null value is encountered on an insert an new number will be generated.

If you need an actual sequence number to be used over several tables then:

CREATE SEQUENCE ORG_SEQ
 START WITH 1
 INCREMENT BY 1
 NO MAXVALUE
 NO CYCLE
 CACHE 24

will define a sequence you then use the "NEXTVAL" keyword anywhere you want the next number in you sql:

NEXTVAL FOR ORG_SEQ
James Anderson
Sorry but this does not fit my requirement. I need to create sequence with a specific start value, so that existing table can use the new sequence (i cant start the sequence at 1 because there are already existing data) and each table have to use a different sequence so that there will not be any "jumping" numbers.
Blake
You can RTFM here http://publib.boulder.ibm.com/infocenter/db2luw/v9//index.jsp . Look at the "identity-options" syntax you find a "STARTS WITH " clause. Also for your requirements for a contiguous sequence you need to set the 'NO CAHCE' option but be aware that this will effectivly "single thread" any updates to the table.
James Anderson