Creating an autoincrement-field

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

Creating an autoincrement-field

Post by formi » Sat 09 Aug 2008 14:17

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

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 18 Aug 2008 12:15

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)

formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

Post by formi » Tue 19 Aug 2008 16:09

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

Post Reply