Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Auto increment column value while inserting

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
kishpra

New User


Joined: 24 May 2010
Posts: 91
Location: Pune

PostPosted: Wed Feb 05, 2014 3:48 pm    Post subject: Auto increment column value while inserting
Reply with quote

Hi,

i want to insert a row in a table where one col emp_id is set as auto increment e.g.

INSERT INTO
(
col1
emp-id
col2
)
VALUES
(:col1
?
:col2)

Please let me know what ? should be replaced with?


Thanks
Back to top
View user's profile Send private message

harithb

New User


Joined: 08 Jun 2009
Posts: 15
Location: Bangalore

PostPosted: Wed Feb 05, 2014 7:54 pm    Post subject:
Reply with quote

Hi Kishpra,

This would require a sequence to be created first and the NEXTVAL function should be used to assign values to the column.

CREATE SEQUENCE employee_id AS INTEGER

Then use

INSERT INTO
(
col1
emp-id
col2
)
VALUES
(:col1
NEXT VALUE for employee_id
:col2)

Regards,
Haritha.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Feb 06, 2014 2:48 pm    Post subject:
Reply with quote

harithb is right if emp-id is a sequence, not so if it is an identity-column
then you would
Code:
INSERT INTO ... ( col1 ,col2 )
VALUES  (:col1 ,:col2)
and emp-id would be handled for you by DB2.

http://db2portal.blogspot.be/2006/09/sequence-objects-and-identity-columns.html
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Loading data to table gives wrong for... Raghu navaikulam DB2 19 Thu Jul 13, 2017 2:11 pm
No new posts Inserting records based on conditions vickey_dw DFSORT/ICETOOL 9 Wed Feb 22, 2017 1:33 pm
This topic is locked: you cannot edit posts or make replies. DB2 query Using Recursion, Converting... smilewithashu2 DB2 3 Tue Jan 03, 2017 12:50 pm
No new posts SQZ pipe delimited file with 1 column... Rick Silvers DFSORT/ICETOOL 6 Sun Nov 06, 2016 8:11 pm
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us