Insert a row into a table with just one column, primary key and id

Here is the table definition:

CREATE TABLE dbo.TableOnlyPK
(
    Id tinyint PRIMARY KEY IDENTITY (1, 1)
)

      

Now I need to insert a row into this table via T-SQL statements: I tried several solutions but none worked.

INSERT INTO dbo.TableOnlyPK () VALUES ()  -- not worked

      

+3


source to share


2 answers


Try:

INSERT INTO dbo.TableOnlyPK DEFAULT VALUES

      

You have created the table below:

CREATE TABLE dbo.TableOnlyPK
(
    Id tinyint PRIMARY KEY IDENTITY (1, 1)
)

      

Each time you run:, INSERT INTO dbo.TableOnlyPK DEFAULT VALUES

you insert one row into the IDENTITY column.



So if you do:

INSERT INTO dbo.TableOnlyPK DEFAULT VALUES
INSERT INTO dbo.TableOnlyPK DEFAULT VALUES
INSERT INTO dbo.TableOnlyPK DEFAULT VALUES

      

This will lead to:

enter image description here

+8


source


INSERT INTO [dbo].[TableOnlyPK]
DEFAULT VALUES

      



+2


source







All Articles