How to insert into a table with just one IDENTITY column?

SqlSql ServerIdentity

Sql Problem Overview


(Came up with this question in the course of trying to answer this other one)

Consider the following MS-SQL table, called GroupTable:

GroupID

1
2
3

where GroupID is the primary key and is an Identity column.

How do you insert a new row into the table (and hence generate a new ID) without using IDENTITY_INSERT ON?

Note that this:

INSERT INTO GroupTable() Values ()   

... won't work.

edit: we're talking SQL 2005 or SQL 2008 here.

Sql Solutions


Solution 1 - Sql

This should work:

INSERT INTO GroupTable DEFAULT VALUES 

Solution 2 - Sql

Here you go:

INSERT INTO GroupTable DEFAULT VALUES

Solution 3 - Sql

It is possible to insert more than one row at a time.

For e.g., to insert 30 rows. INSERT INTO GroupTable DEFAULT VALUES GO 30

This will insert 30 rows by incrementing the identity column each time.

Solution 4 - Sql

Can you try using a Sequence or something similar? Where you select from a Sequence and it will give you the next value in the sequence.

Solution 5 - Sql

This will work actually--

insert into TABLE default values

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncodeulikeView Question on Stackoverflow
Solution 1 - SqlDJ.View Answer on Stackoverflow
Solution 2 - Sqltofi9View Answer on Stackoverflow
Solution 3 - SqlRMKView Answer on Stackoverflow
Solution 4 - SqlMike PoneView Answer on Stackoverflow
Solution 5 - Sqlmanoj rajupetaView Answer on Stackoverflow