Auto increment primary key in SQL Server Management Studio 2012

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Auto increment primary key in SQL Server Management Studio 2012

Message par Clippy »

Auto increment primary key in SQL Server Management Studio 2012
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: Auto increment primary key in SQL Server Management Studio 2012

Message par Clippy »

Make sure that the Key column's datatype is `int` and then setting identity manually, as image shows

Or **just run this code** utilizing [IDENTITY(seed, increment)](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-ver16) modifier

```
-- ID is the name of the [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)

```

the code will run, if `ID` is not the only column in the table

*image reference fifo's*
Répondre

Revenir à « SQL Server »