snippetsqlMajor
How to insert in a table with only an IDENTITY column?
Viewed 0 times
identityinsertcolumnwithhowonlytable
Problem
Given a table with only an IDENTITY column, how do you insert a new row? I've tried the following:
I am testing something and only need the IDENTITY column. It's not for production. Otherwise, such a table can be used as a sequence generator, where no other columns are needed.
INSERT INTO TABLE
(Syntax error)
INSERT INTO TABLE VALUES()
(Syntax error)
INSERT INTO TABLE (Id) VALUES()
(Syntax error)I am testing something and only need the IDENTITY column. It's not for production. Otherwise, such a table can be used as a sequence generator, where no other columns are needed.
Solution
From the documentation:
Forces the new row to contain the default values defined for each column.
So:
In addition:
DEFAULT VALUESForces the new row to contain the default values defined for each column.
So:
INSERT dbo.TABLE DEFAULT VALUES;In addition:
- always use the schema prefix
- always terminate statements with semi-colons
Code Snippets
INSERT dbo.TABLE DEFAULT VALUES;Context
StackExchange Database Administrators Q#40926, answer score: 37
Revisions (0)
No revisions yet.