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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 OlsenDBACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.