Home > Back-end >  How to insert row into a SQL table with auto-increment primary key
How to insert row into a SQL table with auto-increment primary key

Time:10-05

I have a table which holds only one column - an auto-incrementing primary key.

I need to insert values to this table in order to generate new ID.

Here is my table structure

CREATE TABLE [InvestmentJourneys]
(
    [InvestmentJourneyId] [int] NOT NULL IDENTITY(1, 1)
        CONSTRAINT [PK_InvestmentJourneys] 
            PRIMARY KEY CLUSTERED ([InvestmentJourneyId])
) 

I've tried this:

INSERT INTO [InvestmentJourneys] ([InvestmentJourneyId]) 
VALUES (NULL)

But I get an error since this column is the primary key.

I would appreciate any suggestion here on how to achieve this.

CodePudding user response:

Like so:

INSERT INTO dbo.InvestmentJourneys
DEFAULT VALUES;
  • Related