How do I insert a new record to a table containing just an IDENTITY column?

Viewed 55

I have a single-column table where the column is a primary key and clustered index. It is used on other tables to relate records together. It doesn't seem an Insert statement is the way to go, there's no other columns to populate. It's a bit cumbersome to SET IDENTITY_INSERT off and on, etc.

I just need to "increment" the primary key of the table to the next integer value.

I believe it's an easy problem to solve, but I'm at that stage of mental exhaustion where the wheel is still spinning but the hamster is dead.

Here is a script to recreate the table I'm working with.

CREATE TABLE [dbo].[PKOnly]
(
    [Id] [BIGINT] IDENTITY(1,1) NOT NULL,

    CONSTRAINT [PK_PKOnly] 
        PRIMARY KEY CLUSTERED ([Id] ASC)
                WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                      ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY];
1 Answers

You can use DEFAULT VALUES:

INSERT dbo.PKOnly DEFAULT VALUES;

Note this will also work if you have other columns with defaults.

Related