Auto Increment in Insert into Select

Hi,

  I'm on DB2 UDB 8.2.
   I have a table TB_INS_JOB(A INT, B VARCHAR(20),C VARCHAR(10));
   I'm doing the following:

       INSERT INTO TB_INS_JOB(B,C)
        SELECT B,C FROM TB_CP_JOB;

When i insert the values into TB_INS_JOB, i want the values in the column to Auto Increment, for each row inserted.
 
  How can i do it? Please advise.
pvsbandiAsked:
Who is Participating?
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
Hi pvsbandi,

The column that you want to automatically increment must be declared as an IDENTITY column.  DB2 uses the keyword IDENTITY, while other systems often use AUTO_INCREMENT.

  create table TB_INS_JOB
  (
    A INTEGER NOT NULL GENERATED {ALWAYS|BY DEFAULT} AS IDENTITY,
    B VARCHAR(20),
    C VARCHAR(10)
  );

If the IDENTITY column is GENERATED ALWAYS, you can not select a value of your own choosing for the column.  DB2 manages it.

If the IDENTITY column is GENERATED BY DEFAULT, you can choose the value to store when you insert a row or you can let DB2 manage it.


Good Luck,
Kent
0
 
simpsolCommented:
I do not know the exact thing in DB2, but probably example from other database might get you on the right track. The Auto Increment is referred as follows in the following database

Micorosft Access : AutoNumber
Microsoft SQL : Identity
Oracle: Sequence

DB2 might have something similari.

0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi simpsol,

Actually, the DDL for an auto increment column is different for SQL Server, MySQL, DB2, and Oracle.  Just substituting the keyword 'IDENTITY' with 'AUTOINCREMENT' isn't enough.


Kent
0
 
pvsbandiAuthor Commented:
Excellent!! Thanks!
0
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.

All Courses

From novice to tech pro — start learning today.