adding new record in Microsoft SQL server

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
In Microsoft Access, you can set a column to be autonumber, so that when you add a new record, it will automatically increment a key number and use that for the unique record id. In Microsoft SQL, this isn't offered, and I was wondering what would be the best way to get that functionality.

 

DaShen

Lifer
Dec 1, 2000
10,710
1
0
It may be better to use a uniqueidentifier field type and set it to row id.

It is better supported by SQL Server.

You could also use a multiple promary key, using a unique field and a time field as your keys. (this is the best solution)
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Originally posted by: WannaFly
Set a column to int and set it as IDENTITY which will increment it every time.

assuming the column to be autoincremented, by sql server, how would i write an insert statement?

 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
Originally posted by: TechBoyJK
Originally posted by: WannaFly
Set a column to int and set it as IDENTITY which will increment it every time.

assuming the column to be autoincremented, by sql server, how would i write an insert statement?


Just skip the identity column, then use the @@IDENTITY keyword to retrieve the value of the column that was inserted
 

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
ok, so if there is 5 columns total, 1 identity + 4 data, i just insert the data into the 4 data columns, and it autogenerates the identity, as would in access?