Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table and have a lot of records and i can't drop and recreate it
I need to change the increment value of the Id column to 2 the old value is 1

this is an example of the table I need to change the identity increment of

SQL
CREATE TABLE [dbo].[RELATED](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [RELATEDDESC] [nvarchar](50) NOT NULL,
    [USER_ID] [int] NULL,
    [ALTER_DATE] [datetime2](7) NULL,
PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
Posted
Comments
Tomas Takac 19-Oct-15 6:45am    
Did you try ALTER TABLE?
samerselo 19-Oct-15 7:51am    
yes but it gives me error on identity
could you please give me a tested example

1 solution

Try:
SQL
[Id] [int] IDENTITY(2,3) NOT NULL
That will start the value at 2, and increase it by 3 each time:
2
5
8
...
 
Share this answer
 
Comments
samerselo 19-Oct-15 7:49am    
yes
but I need to alter existing table with lots of data

I have no problem creating new table
OriginalGriff 19-Oct-15 8:19am    
If you have lots of data, then you need to be extremely careful - you would be best creating a new table with the new identity rule, transfer all your data, and then remove the original and rename the new.

The only other approach is to drop the existing column, and create a new one - you can't modify an existing IDENTITY specification.

ALTER TABLE [dbo].[MyTable]
DROP COLUMN myColumn
GO
ALTER TABLE [dbo].[MyTable]
ADD myColumn int IDENTITY (2, 5)
GO

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900