IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Auto increment column value while inserting


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
kishpra

New User


Joined: 24 May 2010
Posts: 92
Location: Pune

PostPosted: Wed Feb 05, 2014 3:48 pm
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
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
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.

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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts How to load to DB2 with column level ... DB2 6
No new posts RC query -Time column CA Products 3
No new posts first column truncated in search result IBM Tools 13
No new posts Split a record with data in a differe... DFSORT/ICETOOL 8
Search our Forums:

Back to Top