• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 981
  • Last Modified:

PL/SQL for DB2 to re-initiate the sequences

I want to drop the sequence and create a sequence in DB2, Following is the PL/SQL code I tried but showing error at DECLARE looks like some syntax error...

    TYPE seq_in_cur                     IS REF CURSOR;
    cur_set_seq                         seq_in_cur;
    sql_dyn                             VARCHAR2(2048);    
    v_count                             INTEGER := NULL;    

    sql_dyn := 'SELECT max(ID) from TL_GROUP';
    OPEN cur_set_seq FOR sql_dyn;
    FETCH cur_set_seq INTO v_count;
    v_count := v_count + 1;
    sql_dyn := 'drop sequence GROUP_SEQ';
    sql_dyn := 'create sequence GROUP_SEQ start with ' || v_count || 'increment by 1 NOCACHE NOMAXVALUE NOMINVALUE';
    CLOSE cur_set_seq;

I had some data migration and want to re-initialize the sequences.
  • 3
  • 3
1 Solution
Alexander Eßer [Alex140181]Software DeveloperCommented:
C&P'ed in Oracle DB and it just looked fine, don't know about DB2?!?

What's the error?!
SaggiAuthor Commented:
It works in oracle but in Db2 error:

Parser Messages:
 line 1, col 1: Incorrect syntax near ''
Alexander Eßer [Alex140181]Software DeveloperCommented:
Could it be a problem with invisible characters or blank lines within the code?!
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

SaggiAuthor Commented:
SaggiAuthor Commented:
There was other error:
Lookup Error - DB2 Database Error: ERROR [42601] [IBM][DB2/LINUXX8664] SQL0104N An unexpected token "<space>" was found following "TYPE". Expected tokens may include: "seq_in_cur".
Alexander Eßer [Alex140181]Software DeveloperCommented:
Kent OlsenData Warehouse Architect / DBACommented:
Hi Saggi,

DB2 SQL is different from Oracle SQL (which is different from Microsoft SQL which is different from MySQL SQL, etc...)

In addition, IBM supports three completely different versions of DB2, each with their own code base.  One version for their mainframe, one version for the AS/400, and another version for everyone else.  The required code will change depending on which version of DB2 you're using, and the release level.

That said, the easiest way to do what you're trying to do is with the ALTER SEQUENCE statement.

  ALTER SEQUENCE {sequence_name} RESTART WITH {new_value};

If you need to rename the sequence, you may have to as you suggest and drop and recreate it.  DB2/LUW has no provision for renaming a sequence.  Note that all objects that use the original sequence become invalid.  You'll have to recreate the triggers and other objects that used the sequence.

  DROP SEQUENCE {sequence_name};
  CREATE SEQUENCE {sequence_name} START WITH {first_value};

Also, the statement:

  SELECT max(ID) from TL_GROUP

should throw an error in most releases of DB2 when executed within a procedure.  The correct syntax would be:

  SELECT max(ID) INTO v_count ...

If you're using the sequence to assign unique (incrementing) integer values to each new column in a table, I suggest that you drop the Oracle sequence/trigger style and use the IDENTITY syntax.  It's common to DB2, SQL Server, MySQL and most all engines except Oracle.

Good Luck,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now