Hell-1931
Hell-1931

Reputation: 499

Setting an auto increment value starting from a certain value T-SQL

I have an empty column WHID in my table. I need to update this column with an auto values, where the 1st value will equal 17

So, I expect the result:

WHID:
17
18
19
20
21
22
....
etc.

My code is:

DECLARE @IncrementValue int
SET @IncrementValue = 17

UPDATE ClientEpisode 
SET [WHID] = @IncrementValue + 1 

I get a result of 18 in all rows (which is not what I need).

What do I write in [WHID] = .... or how to modify my code in order to get my expected result?

Upvotes: 1

Views: 110

Answers (1)

Osama Abu Baker
Osama Abu Baker

Reputation: 44

You can use the identity constraint since you build your table as

CREATE TABLE MyTable (WHID INT IDENTITY(17,1))

here you need to drop the existing column & create new column with identity Constraint

Alter tableName Drop Column WHID

then create column with a new constraint

Alter tableName Add WHID Int Identity(17, 1) Go

Upvotes: 1

Related Questions