If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to work with IDENTITY like MSSQL Server in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-28-08, 00:11
nitingautam nitingautam is offline
Registered User
 
Join Date: Nov 2008
Location: Delhi, India
Posts: 15
How to work with IDENTITY like MSSQL Server in DB2

Hi All,

When we work with MSSQL Server we have a concept IDENTITY that we assign to a field and then we will get unique value for that field using command SELECT @@IDENTITY.
How we can do the same thing in DB2?

Regards,
Reply With Quote
  #2 (permalink)  
Old 11-28-08, 02:27
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
If you are doing an insert, just use the term default or leave out the column value for the indentity column.

Assume that table employee has a PK of empno which is an identity column:

insert into employee (empno, name, birthdate) values (default, 'Bob Smith', '1979-08-14');

or

insert into employee (name, birthdate) values ('Bob Smith', '1979-08-14');

If you want to know the value of the indentity value you just inserted, do this:

select empno from final table (insert into employee (empno, name, birthdate) values (default, 'Bob Smith', '1979-08-14'));
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 11-28-08, 03:47
nitingautam nitingautam is offline
Registered User
 
Join Date: Nov 2008
Location: Delhi, India
Posts: 15
Thanks for the solution

I have to create table with IDENTITY
Quote:
create table testIdentity (
userid integer NOT NULL GENERATED BY DEFAULT AS IDENTITY(START WITH 100,INCREMENT BY 1 CACHE 24),
username char(20));


but how to add IDENTITY to existing table field using UPDATE?

Last edited by nitingautam; 11-28-08 at 03:56.
Reply With Quote
  #4 (permalink)  
Old 11-28-08, 04:55
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
You can try the following for an existing table:

ALTER TABLE table-name ADD COLUMN xxx GENERATED ALWAYS AS IDENTITY START WITH .....

Thanks,
Jayanta
Reply With Quote
  #5 (permalink)  
Old 11-28-08, 06:21
nitingautam nitingautam is offline
Registered User
 
Join Date: Nov 2008
Location: Delhi, India
Posts: 15
Quote:
Originally Posted by JAYANTA_DATTA
You can try the following for an existing table:

ALTER TABLE table-name ADD COLUMN xxx GENERATED ALWAYS AS IDENTITY START WITH .....

Thanks,
Jayanta
Thanks for the command Jayanta..but I want to Alter an existing column.

I tried following but not worked
Quote:
ALTER TABLE testIdentity ALTER COLUMN userid GENERATED BY DEFAULT AS IDENTITY START WITH 100 INCREMENT BY 1 ;
Please provide command for that
Reply With Quote
  #6 (permalink)  
Old 11-28-08, 06:52
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
I see. You want to alter the existing field.

You can try this:

ALTER TABLE TEST.TB_DROPME ALTER COLUMN EMPID
SET GENERATED ALWAYS AS IDENTITY ( START WITH 1, INCREMENT BY 1, NO CYCLE, CACHE 20 ) ;

You need to ensure that in the existing table the field EMPID is NOT NULL and BIGINT.

Thanks,
Jayanta
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On