Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

I have an existing table that I am about to blow away because I did not create it with the ID column set to be the table's identity column.

Using SQL Server Management Studio, I scripted a "Create To..." of the existing table and got this:

CREATE TABLE [dbo].[History](

    [ID] [int] NOT NULL,

    [RequestID] [int] NOT NULL,

    [EmployeeID] [varchar](50) NOT NULL,

    [DateStamp] [datetime] NOT NULL,

 CONSTRAINT [PK_History] 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]

My question is, how would I modify this SQL so that my resulting table has the ID column set as the Identity?

1 Answer

0 votes
by (40.4k points)

Use the following code:

CREATE TABLE [dbo].[History](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [RequestID] [int] NOT NULL,

    [EmployeeID] [varchar](50) NOT NULL,

    [DateStamp] [datetime] NOT NULL,

 CONSTRAINT [PK_History] 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]

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...