Page 1 of 1

Creating an autoincrement-field

Posted: Sat 09 Aug 2008 14:17
by formi
How can I create an autoincrement-field with SQL? Have I to use triggers? Has anybody a code-example for that.

Thank's a lot, Peter

Posted: Mon 18 Aug 2008 12:15
by Dimon
To create autoincrement field you should use the IDENTITY option for relevant field. You can use the following code:

Code: Select all

CREATE TABLE TableName(
  FieldName int IDENTITY(1,1) NOT NULL PRIMARY KEY)

Posted: Tue 19 Aug 2008 16:09
by formi
Thank's a lot. It works an I just fouind the way to convert data from old database without loosing the identity. For thouse who are intereseted in

set identity_insert TableName on

Convert all data an then

set identity_insert TableName off